Page 1 of 1

[SOLVED] I need to bold keywords with regex...

Posted: Sat Oct 09, 2004 7:05 am
by tomfra
I am using this code to bold keywords in a string named $description:

Code: Select all

$description = @eregi_replace(" $kw", " <b>$kw</b>", $description);
It works, but if the keyword is found in description, it will ignore its case so for example keyword "PHP" becomes "<b>php</b>". I'd like to keep the case of the keyword it finds. I guess it's possible with regex?

Any idea?

Thanks!

Tomas

Posted: Sat Oct 09, 2004 8:04 am
by twigletmac
Try this [php_man]preg_replace[/php_man]():

Code: Select all

$description = preg_replace('/(\b'.$kw.'\b)/i', '<b>\\1</b>', $description);
Mac

Posted: Sat Oct 09, 2004 9:20 am
by feyd
I'd also use [php_man]preg_quote[/php_man]() on $kw on each pass.

Posted: Sat Oct 09, 2004 9:40 am
by tomfra
Thanks! Everything seems to be working.

Tomas