Jump to content


Photo

[SOLVED] Searching to alike words


  • Please log in to reply
2 replies to this topic

#1 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 July 2006 - 04:15 AM

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:
<?
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;
}
?>

I have stored some informations in MySQL database, and I echo them out, it will look like this:

Color
Blue Colour
Gold Colour
Red Colour


And let's say I have use the function when I echo those informations,
bbcode($row['color'])

Then the outcome will be messed up, it will become this:

Color
Blue Color
Gold Color
Red Color


Is there something need to be added in regex so that it will echo out normally ?
Actually I want it to echo out

Color
Blue Color
Gold Color
Red Color


Hope you understand what I'm trying to say, thanks.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 01 July 2006 - 04:37 PM

Either put your more specific patterns first; e.g., "Red Colour" is more specific than "Colour," or anchor your expressions with ^ and $ (/^Colour$/).
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 02 July 2006 - 01:08 AM

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:

if (preg_match("/^Colour\b/", $text)) {
$pattern[] = '/Colour/';
$replace[] = '<a href="colour.php">Colour</a>';
} else {
// The rest of the scripts.
}

Problem solved, thanks again.  ;)

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users