Hiya.
I'm having a bit of a problem with a function I found and heavily modified for my own use that highlights the search terms that a person has entered into a search form. I recently added the ability to do exact phrase searches, but now the highlighter needs to be adjusted accordingly because it tends to highlight things as if they were wildcarded. It's a little hard to explain, so I'll describe what it's doing now and what I want it to do.
First, the function itself:
function callback($buffer) {
global $search;
if(strstr($search,"\"")) {
$word = str_replace("\"", "", stripslashes($search));
$buffer = preg_replace('|('.quotemeta($word).')|iU',
'<span class=\'highlight\'>\\1</span>', $buffer);
} else {
$words = explode(' ', $search);
foreach( $words AS $word ){
$buffer = preg_replace('|('.quotemeta($word).')|iU',
'<span class=\'highlight\'>\\1</span>', $buffer);
}
}
return $buffer;
}
It's applied like so:
ob_start("callback"); starts the highlighting and ob_end_flush(); ends it . . .
let's say a person were to search for the word "end".
the highlighter will be applied on the words end, ending, send, etc.
I want it so that it only highlights the exact term that the person has searched for. If a person searches for "fire", it will display the results then the highlighter goes to work and highlights only the word "fire" throughout the results and not the words fireball, hellfire, fired, etc.
And of course, I only want to apply it to this part of the function:
if(strstr($search,"\"")) {
$word = str_replace("\"", "", stripslashes($search));
$buffer = preg_replace('|('.quotemeta($word).')|iU',
'<span class=\'highlight\'>\\1</span>', $buffer);
Any ideas?
Thanks in advance.
--Tony