wfareed Posted August 16, 2012 Share Posted August 16, 2012 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_count=count($links); for($i=0;$i<$link_count;$i++){ $link_url=preg_replace("/\[link]/", "", $links[0][$i]); $link_url=preg_replace("/\[\/link]/","",$link_url); $text=str_replace($links[0][$i],"<a href=\"" . $link_url . "\">" . $link_url . "</a>",$text); } echo $text; Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 16, 2012 Share Posted August 16, 2012 You're over complicating this a bit. All you need is one preg_replace. $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]"; $search = '/\[link\](.*?)\[\/link\]/si'; $replace = '<a href="$1">$1</a>'; $text = preg_replace($search, $replace, $text); Quote Link to comment Share on other sites More sharing options...
wfareed Posted August 16, 2012 Author Share Posted August 16, 2012 That is great and works prefectely , One more question , What if i need to include something like for bold text or for italic and so on , how can i combine all in one $search variable ? Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 16, 2012 Share Posted August 16, 2012 You will need to use an array. You'll have to use an array for $replace too, and the keys have to match up. $search = array( '/\[link\](.*?)\[\/link\]/si', '/\[b\](.*?)\[\/b\]/si', '/\[i\](.*?)\[\/i\]/si', ); $replace = array( '<a href="$1">$1</a>', '<b>$1</b>', '<i>$1</i>', ); Quote Link to comment Share on other sites More sharing options...
wfareed Posted August 16, 2012 Author Share Posted August 16, 2012 Man ! U r brilliant , saved me a lot of time with a very simple code , I was pulling my hair out trying to figure it out , really appreciate your help a lot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.