I have the following, simple code:
$text = str_replace($f,'<a href="'.$f.'" target="_blank">'.$u.'</a>',$text);
where $f is a URL, like http://google.ca, and $u is the name of the URL (my function names it ‘Google’).
My problem is, is if I give my function a string like
http://google.ca http://google.ca
it returns
<a href="<a href="http://google.ca" target="_blank">Google</a>" target="_blank">Google</a> <a href="<a href="http://google.ca" target="_blank">Google</a>" target="_blank">Google</a>
Which obviously isn’t what I want. I want my function to echo out two separate, clickable links. But str_replace is replacing the first occurrence (it’s in a loop to loop through all the found URLs), and that first occurrence has already been replaced.
How can I tell str_replace to ignore that specific one, and move onto the next? The string given is user input, so I can’t just give it a static offset or anything with substr, which I have tried.
Thank you!
One way, though it’s a bit of a kludge: you can use a temporary marker that (hopefully) won’t appear in the string:
That way, the first substitution won’t be found again. Then at the end (after you’ve processed the entire line), simply change the markers back: