OK, here is a quick example of how to highlight search results KEEPING CASE. (I.e., user searches for "WEB" highlight "Web" and "web" and do not change the case of the results....)
Keeping case is one of those weird, undocumented (or hard to find documented) things. I've run into this issue with PHP, Perl and Cold Fusion. Always a strain to find the correct string to replace without changing case.
That said - assume user is searching ($keyword); replace text in the title ($r_title) and description ($r_description) of results. I've used hard-coded color values here for clarity; a better way is to wrapt the result in a class - but note the key part: "\0" in the regular expression is key to keeping the case:
// And now let's do a search and replace to highlight the keyword(s)
if ($keyword) {
$r_title[$j] = eregi_replace($keyword, "<span style=background-color:#99CCFF;>\\0</span>", $r_title[$j]);
$r_description[$j] = eregi_replace($keyword, "<span style=background-color:#99CCFF;>\\0</span>", $r_description[$j]);
}
}
You can build a function to do this (so you can more quickly/cleanly highlight text in many areas with simple function call; this code is the basic basis of such a function.
Enjoy!