Startup

Technology Review

Tips

Video

Welcome

Home » Technology Review

PHP: Highlight a keyword in a phrase

Submitted by admin on Wednesday, 27 August 2008No Comment

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 ) );
}

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.