Converting text URLS into real clickable links

I am trying to create a script that will help me convert urls provided by users in their blog posts of PM to other users into a real clickable links , The script i created is working fine except that it is not converting after the 2nd link occurred in the post , I hope someone can help me with that.
Here is the code :

$text="Click [link][/link] or click [link][/link] or click [link][/link]";

preg_match_all('/\\\\[link](.*?)\\\\[\\/link]/s', $text, $links);

    $link_url=preg_replace("/\\[link]/", "", $links[0][$i]);
    $text=str_replace($links[0][$i],"<a href=\\"" . $link_url . "\\">" . $link_url . "</a>",$text);

echo $text;

I think it is a clash of double/single quotes …

$text=str_replace($links[0][$i],'<a href=\\"' . $link_url . '\\">' . $link_url . '</a>'$text); 

Thanks a lot for your help , appreciate it

You’re accessing the wrong portion of the array returned by preg_match_all(). $link[0] contains the entire match, not the just the link you’re targeting with the regex subpattern. Loop through $link[1]. That has all the URL’s you want, already stripped of formatting. There’s no need to run two more regular expressions as well as the str_replace().

Also, your regex expression could be tidied up a bit.

Here you go:

preg_match_all('~\\[link](.*?)\\[/link]~s', $text, $links); 
$text = '';
foreach($links[1] as $key => $link)
	$text .= "<a href=\\"" . $link . "\\">" . $link . "</a> ";

Thanks alot , That was pretty helpful :slight_smile:

Hi I’m new to the forum and posted the same question.
I’m also new to PHP and would like to use your script but not sure how to add it to my stie.