placeholder php question

They have: 2,390 posts

Joined: Nov 1998

Hi guys,

I have a question which goes beyond was a str_replace can do.

I have a form that users populate with txt and php then formats it.

i want to add functionality so that if they type

[click here:http://www.website.com]

php will turn that into a normal html link <a href="http://www.website.com">click here</a>
'

anyone know how i can do this?

Thanks a lot

JP

cdwhalley.com's picture

He has: 3 posts

Joined: Apr 2006

I think this should do it, although I haven't tested it.

<?php
$input_string
= \"[click here:http://www.website.com]\";//this is from the form
$first_bracket = strpos($input_string, \"[\") + 1;//get the position of the first bracket and go 1 past it
$last_bracket = strrpos($input_string, \"]\");//get the position of the last bracket
$substr_length = $last_bracket - $first_bracket;//get the length of between the brackets
$input_string = substr($input_string, $first_bracket, $substr_length);//cut out the inside of the brackets
list(
$link_text, $link_url) = explode(\":\", $input_string);//get the data from either side of the :
echo \"<a href=\\"
\".$link_url.\"\\">\".$link_text.\"</a>\";//output
?>

This would only work if there was just 1 of these links in the $input_string. It would need to be more complex if there was more than 1.

EDIT: preg_replace would probably be more appropriate, but my regex isn't up to that standard Smiling

They have: 2,390 posts

Joined: Nov 1998

thanks

it does need to happen more than once

and i think your right i looked at preg replace and it seems that will work. can anyone confirm that preg replace will do the job before i spend some time on it?

thanks

JP

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.