function highlightKeyword($text, $keyword) {
$keywordLower = strtolower($keyword);
$textLower = strtolower($text);
$pos = strpos($textLower, $keywordLower);
while ($pos !== false) {
$start = substr($text, 0, $pos);
$end = substr($text, $pos + strlen($keyword));
$text = $start . "<mark>" . substr($text, $pos, strlen($keyword)) . "</mark>" . $end;
$textLower = strtolower($text);
$pos = strpos($textLower, $keywordLower);
}
return $text;
}
$searchTerm = 'cctv';
$text = 'This is an example text where the keyword CCTV is found.';
$highlightedText = highlightKeyword($text, $searchTerm);
echo $highlightedText;
|