function highlightkeyword($str, $search) {
if($search){
$highlightcolor = "#FFCC33";
$occurrences = substr_count(strtolower($str), strtolower($search));
$newstring = $str;
$match = array();
for ($i=0;$i<$occurrences;$i++) {
$match[$i] = stripos($str, $search, $i);
$match[$i] = substr($str, $match[$i], strlen($search));
$newstring = str_replace($match[$i], '[#]'.$match[$i].'[@]', strip_tags($newstring));
}
$newstring = str_replace('[#]', '<span style="background-color:'.$highlightcolor.'">', $newstring);
$newstring = str_replace('[@]', '</span>', $newstring);
return $newstring;
}else{
return $str;
}
}
Credit: http://www.webdevdoor.com/php/highlight-search-keyword-string-function/
No comments:
Post a Comment