Page 1 of 1

String Highlighter....

Posted: Mon Oct 10, 2005 8:30 pm
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

Posted: Mon Oct 10, 2005 9:06 pm
by feyd
use preg_replace() and group-references

Code: Select all

$text = preg_replace('#('.preg_quote($post,'#').')#i','<strong>\\1</strong>',$text);

Posted: Mon Oct 10, 2005 9:17 pm
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

Posted: Mon Oct 10, 2005 9:22 pm
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.

Posted: Mon Oct 10, 2005 9:27 pm
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

Posted: Mon Oct 10, 2005 9:30 pm
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

Posted: Mon Oct 10, 2005 9:33 pm
by feyd
you may want to read through our own Regex board as well:

viewforum.php?f=38

Posted: Mon Oct 10, 2005 9:39 pm
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