My apologies. I'm a PHP newbie and the count function didn't work right if you have more than one "keyword". I searched and spent a long time to get this to work right, so wanted to share it.
This is very easy to use by doing the following.
$text - is the text to search
$words - are the words to highlight (search text)
$the_place - is so that you can tell your users what "area" was searched.
<?php
function highlight_this($text, $words, $the_place) {
$words = trim($words);
$the_count = 0;
$wordsArray = explode(' ', $words);
foreach($wordsArray as $word) {
if(strlen(trim($word)) != 0)
$exclude_list = array("word1", "word2", "word3");
if ( in_array( strtolower($word), $exclude_list ) ) {
} else {
$text = str_ireplace($word, "<span class=\"highlight\">".strtoupper($word)."</span>", $text, $count);
$the_count = $count + $the_count;
}
}
echo "<br><div class=\"emphasis\">A search for <strong>" . $words. "</strong> found <strong>" . $the_count . "</strong> matches within the " . $the_place. ".</div><br>";
return $text;
}
$text_to_highlight = highlight_this($text_to_highlight, $search_text, "Place you searched");
?>