note 104676 modified in function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Thu, 30 Jun 2011 13:42:15 +0000 |
| Subject: | note 104676 modified in function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181099@lists.php.net to get a copy of this message | ||
For highlighting without the overhead of regex and without destroying capitalization, try this:
<?php
function highlight($needle, $haystack){
$ind = stripos($haystack, $needle);
$len = strlen($needle);
if($ind !== false){
return substr($haystack, 0, $ind) . "<b>" . substr($haystack, $ind, $len) .
"</b>" .
highlight($needle, substr($haystack, $ind + $len));
} else return $haystack;
}
?>
This example uses HTML bold tags, but you can easily change the highlighting method.
--was--
For highlighting without the overhead of regex and without destroying capitalization, try this:
function highlight($needle, $haystack){
$ind = stripos($haystack, $needle);
$len = strlen($needle);
if($ind !== false){
return substr($haystack, 0, $ind) . "<b>" . substr($haystack, $ind, $len) .
"</b>" .
highlight($needle, substr($haystack, $ind + $len));
} else return $haystack;
}
This example uses HTML bold tags, but you can easily change the highlighting method.
http://php.net/manual/en/function.str-ireplace.php