Hello, I've been trying to figure out how I highlight the keyword of a simple search for a project I have been working on. With the following code, I can get the keyword highlighted, but I want the search results to come back in a style similar to Google, where the search result is chopped up just to show you the found keyword in various parts of a database article. Thanks for any help, or pointers!
function highlight( $needle, $haystack )
{
$parts = explode( strtolower($needle), strtolower($haystack) );
$pos = 0;
foreach( $parts as $key=>$part ){
$parts[ $key ] = substr($haystack, $pos, strlen($part));
$pos += strlen($part);
$parts[ $key ] .= '<FONT COLOR="#ff0000"><B>'.substr($haystack, $pos, strlen($needle)).'</B></FONT>';
$pos += strlen($needle);
}
return( join( '', $parts ) );
}