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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

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

Post 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
Last edited by tomfra on Sat Oct 09, 2004 9:40 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd also use [php_man]preg_quote[/php_man]() on $kw on each pass.
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

Thanks! Everything seems to be working.

Tomas
Post Reply