wfareed — 2012-08-16T17:07:54-04:00 — #1
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);
cups — 2012-08-16T19:05:29-04:00 — #2
I think it is a clash of double/single quotes ...
$text=str_replace($links[$i],'<a href=\\"' . $link_url . '\\">' . $link_url . '</a>'$text);
wfareed — 2012-08-16T19:27:33-04:00 — #3
Thanks a lot for your help , appreciate it
brandonbuster — 2012-08-16T19:31:32-04:00 — #4
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> ";
wfareed — 2012-08-16T19:32:44-04:00 — #5
Thanks alot , That was pretty helpful
cisco115 — 2012-10-17T11:06:08-04:00 — #6
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.