Jump to content

Archived

This topic is now archived and is closed to further replies.

hackerkts

[SOLVED] Searching to alike words

Recommended Posts

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]Color
Blue Colour
Gold Colour
Red 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.

Share this post


Link to post
Share on other sites
Either put your more specific patterns first; e.g., "Red Colour" is more specific than "Colour," or anchor your expressions with ^ and $ (/^Colour$/).

Share this post


Link to post
Share on other sites
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.  ;)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.