giraffemedia Posted August 19, 2011 Share Posted August 19, 2011 Hi, I'm trying to write a function that finds all text within a string beginning with [[ and ending with ]] and manipulates the content between them. For example: This is some sample text and [[Debenhams||debenhams.com]] and this is a bit more sample text that [[sainsburys||sainsburys.com]] What I want to do is turn the bits between the brackets into links, using the first piece of text as the link name and the second as the link url. What I need is a function that replaces every instance of the bracketed code with the link text. Here is what I have so far which is working. function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $fullstring = "Here is some link text [[Debenhams||debenhams.com]] my"; $parsed = explode("||", get_string_between($fullstring, "[[", "]]")); echo '<a href="http://www.'.$parsed[1].'" title="Visit the '.$parsed[0].' website" target="_blank">'.$parsed[0].'</a>'; // (result = link to Debenhams) What I cannot figure out is how to write a function that searches a string and replaces all the bits between the brackets with the links. I'd really appreciate some help please. Quote Link to comment https://forums.phpfreaks.com/topic/245194-replace-each-occurence-between-brackets-with-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2011 Share Posted August 19, 2011 You would use preg_replace to directly do this - <?php $str = "This is some sample text and [[Debenhams||debenhams.com]] and this is a bit more sample text that [[sainsburys||sainsburys.com]]"; $str = preg_replace("/\[\[(.*?)\|\|(.*?)\]\]/is","<a href='http://www.$2' title='Visit the $1 website' target='_blank'>$1</a>",$str); echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/245194-replace-each-occurence-between-brackets-with-code/#findComment-1259391 Share on other sites More sharing options...
giraffemedia Posted August 19, 2011 Author Share Posted August 19, 2011 You sir, are a bloody genius! Do you know of somewhere I can learn about regex apart from the php online manual, I find that too complicated Thanks for your help, James Quote Link to comment https://forums.phpfreaks.com/topic/245194-replace-each-occurence-between-brackets-with-code/#findComment-1259394 Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2011 Share Posted August 19, 2011 See the various sticky posts in this forum section - http://www.phpfreaks.com/forums/index.php?board=43.0 Quote Link to comment https://forums.phpfreaks.com/topic/245194-replace-each-occurence-between-brackets-with-code/#findComment-1259396 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.