hackerkts Posted July 1, 2006 Share Posted July 1, 2006 I'm new with regex, so please be alittle patient.This question might be really noob, but it's complcated for me. :-\I'm trying to use preg_replace and regex in a function, here's an example of the function:[code]<?function bbcode($text){$pattern[] = '/Colour/i';$replace[] = '<a href="colour.php">$1</a>';$pattern[] = '/Red Colour/i';$replace[] = '<a href="redcolour.php">$1</a>';$pattern[] = '/Blue Colour/i';$replace[] = '<a href="bluecolour.php">$1</a>';$pattern[] = '/Gold Colour/i';$replace[] = '<a href="goldcolour.php">$1</a>';$text = preg_replace($pattern, $replace, $text);return $text;}?>[/code]I have stored some informations in MySQL database, and I echo them out, it will look like this:[quote]ColorBlue ColourGold ColourRed Colour[/quote]And let's say I have use the function when I echo those informations,[code]bbcode($row['color'])[/code]Then the outcome will be messed up, it will become this:[quote][url=http://colour.php]Color[/url]Blue [url=http://colour.php]Color[/url]Gold [url=http://colour.php]Color[/url]Red [url=http://colour.php]Color[/url][/quote]Is there something need to be added in regex so that it will echo out normally ?Actually I want it to echo out[quote][url=http://colour.php]Color[/url][url=http://colour.php]Blue Color[/url][url=http://colour.php]Gold Color[/url][url=http://colour.php]Red Color[/url][/quote]Hope you understand what I'm trying to say, thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 1, 2006 Share Posted July 1, 2006 Either put your more specific patterns first; e.g., "Red Colour" is more specific than "Colour," or anchor your expressions with ^ and $ (/^Colour$/). Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 2, 2006 Author Share Posted July 2, 2006 Thanks for replying and sorry for me late reply, last night until now I keep trying to get it working.Heh.. Finally found a solution, I used \b with if and else statements.Example:[code]if (preg_match("/^Colour\b/", $text)) {$pattern[] = '/Colour/';$replace[] = '<a href="colour.php">Colour</a>';} else {// The rest of the scripts.}[/code]Problem solved, thanks again. ;) 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.