I am working with the following function, not written by myself. The function is part of a search engine that finds the query word in the database and returns a strip of text with the query word bolded (as $desc). (like Google)...
However, this engine was built to only search for words using the wildcard, so it does not have the capability to search for more than one word (except as a phrase)... until yesterday. I added some code that allows this search engine to search the database Boolean style (e.g. "yellow string -rope"). It works great and returns the correct results, but it will not display this particular function correctly. If the search contains only one word, then it returns the $desc just fine with the query word bolded. But if you search for two words (e.g. "Yellow +string"), while it will return the correct number of results, it leaves out the $desc because it doesn't know how to bold more than one word from a query.
With that background info (whew!)... The query words are contained in $q_array
I think what I need to do is modify this function using a foreach() with the $q_array, telling it to bold each word from $q_array... but I don't know where (or even exactly how) to put it. I've tried it in various places, either with no results or an error.
Anyone have any idea how I can modify this funtion to loop through the $q_array and bold each word from the query when the query contains more than one word? (That's alot to take in...)
?>
function processWindow(&$content, $width, $height, $q, $matchCount) {
global $config;
$lowQ = strtolower($q);
$qSize = strlen($q);
$window = $width / 2;
$descEnd = $config['descEnd'];
$boldQuery = $config['boldQuery'];
$stripTags = $config['stripTags'];
$desc = array();
$content = str_replace('<br', ' <br', $content);
$content = str_replace('<Br', ' <Br', $content);
$content = str_replace('<BR', ' <BR', $content);
$content = str_replace('<p', ' <p', $content);
$content = str_replace('<P', ' <P', $content);
$content = str_replace(' ', ' ', $content); //' ' parts show up if they are broken, so convert to spaces.
if ($stripTags)
$content = strip_tags($content);
$content = ereg_replace('[[:space:]]{2,}', ' ', $content); //Consume extra whitespace from content.
$content = trim($content);
$lowContent = strtolower($content);
$contentSize = strlen($content);
$queryIdx = 0;
for ($i = 0; $i < $height; $i++) {
$queryIdx = strpos($lowContent, $lowQ, $queryIdx);
if (!is_int($queryIdx) || $queryIdx >= $contentSize) //If we don't find $q, then there are no more matches.
break;
/* Grab the section of text around the matching keyword. */
$lBound = ($queryIdx - $window < 0) ? 0 : $queryIdx - $window;
$uBound = ($lBound + $width > $contentSize) ? $contentSize : $lBound + $width;
$lBound = ($uBound < $contentSize) ? $lBound : (($uBound - $width < 1) ? 0 : $uBound - $width);
/* Slide our window to avoid cutting words. */
$descAdj = $uBound - ($queryIdx + $qSize);
for ($j = 0; $j < $descAdj; $j++) {
if ($lBound - $j <= 0 || $content[$lBound - $j - 1] == ' ') {
$lBound -= $j;
$uBound -= $j;
break;
}
}
/* Shrink the uBound to avoid cutting words. */
$descAdj = $uBound - ($queryIdx + $qSize);
if ($uBound < $contentSize && $content[$uBound] != ' ') {
for ($j = 1; $j < $descAdj; $j++) {
if ($content[$uBound - $j] == ' ' && $uBound - $j > $lBound) {
$uBound -= $j;
break;
}
}
}
/* Cut the desc out of content and add descEnd. */
$q_array = explode(" ", $q);
$descBuf = '';
if ($lBound > 0)
$descBuf = $descEnd;
$descBuf .= trim(substr($content, $lBound, $uBound - $lBound));
if ($uBound < $contentSize)
$descBuf .= $descEnd;
if (!$boldQuery)
$desc[$i] = $descBuf;
else
$desc[$i] = eregi_replace("($q)","<B>\\1</B>", $descBuf);
$queryIdx = $uBound; //Jump the queryIdx to the end of the desc.
}
return $desc;
}
?>