Jump to content

[SOLVED] Searching to alike words


hackerkts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.