ok, this should work better
(phpbuilder chomped the font tags )
if it doesn't, use common sense as
to where to change :0
-kyle
/
Highlights all occurances within $string
note: $words is an array
you can change a string into an array easily:
$keywords = array();
$keywords[] = trim($searchwords);
highlight before/after are optional (defaults to bolding)
Example: highlight_text($keywords, $quote,
"<font style =\"text-decoration: none; color: #CC3333; background: #ffa; \">", "</font>")
*/
function highlight_text( $words, $string,
$highlight_before = "<b>", $highlight_after = "</b>" )
{
$tempquote = "";
$changed = 0;
/* tstring is what we'll be working with */
$tstring = $string;
/* loop through each search word */
for ($i = 0; $i < count($words); $i++ )
{
$newstring = "";
while ( ($pos = strpos($tstring, $words[$i])) != false )
{
/* hold everything up to the match */
$posbehind = substr($tstring, 0, $pos);
/* and everything after the word.. */
$posahead = substr($tstring, $pos+strlen($words[$i]));
/* now concatenate */
$newstring .= $posbehind . $highlight_before . $words[$i] . $highlight_after;
/* update tstring */
$tstring = substr($tstring, $pos+strlen($words[$i]));
$changed = 1;
if ( strpos($posahead, $words[$i]) === false )
$newstring .= $posahead;
}
/* prep for next word */
$tstring = $newstring;
}
if ( $changed == 0 )
return $string;
return $newstring;
}