String Highlighter....

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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

String Highlighter....

Post by Zoxive »

Well i made a search field for my site, and now i'm trying to make it highlight the search terms once they were found.

This works, but it it replaces the search term ($post) completely, say i search for "WhAt" and in $line['content'] it has what, it would make it bold, like i want, but it will also replace it with "WhAt".

Code: Select all

$line['content'] = eregi_replace($post, "<strong>" . $post . "</strong>", $line['content']);
Thanks : p

-NSF
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use preg_replace() and group-references

Code: Select all

$text = preg_replace('#('.preg_quote($post,'#').')#i','<strong>\\1</strong>',$text);
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Thanks, is there any place other then php.net that explains this?

I have the chm manual, and it doesn't help me much to understand it...

-NSF
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Sounds like you need to learn about regular expressions. I find this php.net page is VERY helpful, but a quick google for "Perl Regular Expressions" will come up with loads of suggestions.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Ambush Commander wrote:Sounds like you need to learn about regular expressions. I find this php.net page is VERY helpful, but a quick google for "Perl Regular Expressions" will come up with loads of suggestions.
Thanks, i know php.net is very usefull, just i didn't understand it the way they put it, but now i see theres a different side, Regular expressions.

-NSF
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yes, it's really quite a bugger when you need to do something but you don't know its name. The name is powerful thing, yes. O_O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to read through our own Regex board as well:

viewforum.php?f=38
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

feyd wrote:you may want to read through our own Regex board as well:

viewforum.php?f=38
Nice, didn't even notice it. : p

-NSF
Post Reply