U kunt deze code gebruiken:
<?php
//$keyword -> keyword to be search in string.
//$field -> string contain value to be seach
// Returns HTML content with highlighted text
function makeHighlighter($keyword, $field) {
$i = strripos($field, $keyword);
if ($i !== false) {
$keyword = str_ireplace($keyword, substr($field, $i, (strlen($keyword))), $keyword);
} else {
return $field;
}
$as_unm_split = explode($keyword, $field);
$string_hig = "";
for ($i = 0; $i < count($as_unm_split); $i++) {
if ($i < count($as_unm_split) - 1)
$string_hig.=$as_unm_split[$i] . "<span style=\"color: red\">" . $keyword . "</span>";
else
$string_hig.=$as_unm_split[$i];
}
unset($as_unm_split, $keyword, $i);
return $string_hig;
}
echo makeHighlighter("keyword","this is keyword to be match...");
?>
O/P
dit is zoekwoord om overeen te komen...
[Opmerking:"trefwoord" wordt in RODE kleur]