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]http://www.google.com.eg[/link] or click [link]http://www.yahoo.com[/link] or click [link]http://www.hotmail.com[/link]";
preg_match_all('/\\\\[link](.*?)\\\\[\\/link]/s', $text, $links);
$link_url=preg_replace("/\\[link]/", "", $links[$i]);
$text=str_replace($links[$i],"<a href=\\"" . $link_url . "\\">" . $link_url . "</a>",$text);
I think it is a clash of double/single quotes …
$text=str_replace($links[$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 contains the entire match, not the just the link you’re targeting with the regex subpattern. Loop through $link. 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 as $key => $link)
$text .= "<a href=\\"" . $link . "\\">" . $link . "</a> ";
Thanks alot , That was pretty helpful
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.