Highlighting search words

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
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Highlighting search words

Post by leebo »

Hi

I am wanting to highlight the words that the user searches for:

<?php
if(!isset($searchItem)) { $searchItem = "Orange"; }
function callback($buffer) {

global $searchItem;
// surround search item items with highlight class
return (ereg_replace($searchItem, "<span class='highlight'>$searchItem</span>", $buffer));

}

ob_start("callback");

?>

<html>
<style>
.highlight {
background: #44AA44;
color: white;
}
</style>
<body>
<p>It's like comparing bannanas to oranges to apples to peaches.
</body>
</html>


This works fine how ever if the user seached Orange instead of orange ( uppercase for the first letter ) then it does not work.
Does anyone know how to make it so it highlights the word either case

Thanks
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

eregi_replace() -- case insensitive replacement.
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

Great Stuff it works

As easy as adding one char :D

I was looking for the answer for over 1 hour but could not find it !

Thanks a lot
Post Reply