Gooner Posted July 22, 2010 Share Posted July 22, 2010 Hi I have searched the forums for 2 hours and cannot find what I am looking for. Apologies if the answer is already here. I need to write a php regular expression that will change any text within square brackets to a link. Example "Some text here. But look, a part ref number of [AK123] and another one just here [AK124]. that is all." From this text, I need to add <a href> tags to the above, so that AK123 and AK124 link to http://www.mysite.com/ak123.php and http://www.mysite.com/ak123.php respectively. I also need the square brackets to go (the brackets are there to indicate the text that needs linking). The result I need is: "Some text here. But look, a part ref number of AK123 and another one just here AK124. that is all." Can anyone help on this one? Many Thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 22, 2010 Share Posted July 22, 2010 $text = "Some text here. But look, a part ref number of [AK123] and another one just here [AK124]. that is all."; $text = preg_replace('~\[([a-z0-9]+)\]~ie', '"<a href=\"http://site.com/".strtolower(\'\\1\').".php\">".strtoupper(\'\\1\')."</a>"', $text); echo $text; Quote Link to comment Share on other sites More sharing options...
Gooner Posted July 23, 2010 Author Share Posted July 23, 2010 That works brilliantly. Thank you so much for your time and expertise. 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.