Thursday, 14 August 2008
PHP Routine to highlight a keyword in any given phrase:
function highlight( $phrase , $keyword)
{
$parts = explode( strtolower($needle), strtolower($phrase) );
$pos = 0;
foreach( $parts as $key=>$part )
{
$parts[ $key ] = substr($phrase, $pos, strlen($part));
$pos += strlen($part);
$parts[ $key ] .= ''.substr($phrase, $pos, strlen($keyword)).'';
$pos += strlen($keyword);
}
return( join( '', $parts ) );
}
Related
- ASP: Teaser Text
- J2ME: Bounds checking of a List
- PHP: Encoding HTML Special Characters
- Java: Mix up items in array
- PHP: Web Service Feed Information


