note 100539 modified in function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Fri, 22 Oct 2010 04:22:47 +0000 |
| Subject: | note 100539 modified in function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173364@lists.php.net to get a copy of this message | ||
FIX-ed problem with highlighting second 'o' OR 'a', in this string
<?php
function highlight_string ($haystack, $needle, $highlight_class) {
// return $haystack if there is no highlight color or strings given, nothing to do.
$first_encode='XXXXXXXXXXXXXXX'; //ENCODE string
$second_encode='YYYYYYYYYYYYYYY';
preg_match_all("/$needle+/i", $haystack, $matches);
if (is_array($matches[0]) && count($matches[0]) >= 1) {
foreach ($matches[0] as $match) {
$haystack = str_replace($match, $first_encode.$match.$second_encode, $haystack);
}
}
$haystack=str_replace(array($first_encode,$second_encode),
array('<font class="'.$highlight_class.'"
>','</font>'),$haystack);
return $haystack;
}
?>
--was--
FIX-ed problem with highlighting second 'o' OR 'a', in this string
function highlight_string ($haystack, $needle, $highlight_class) {
// return $haystack if there is no highlight color or strings given, nothing to do.
$first_encode='XXXXXXXXXXXXXXX'; //ENCODE string
$second_encode='YYYYYYYYYYYYYYY';
preg_match_all("/$needle+/i", $haystack, $matches);
if (is_array($matches[0]) && count($matches[0]) >= 1) {
foreach ($matches[0] as $match) {
$haystack = str_replace($match, $first_encode.$match.$second_encode, $haystack);
}
}
$haystack=str_replace(array($first_encode,$second_encode),
array('<font class="'.$highlight_class.'"
>','</font>'),$haystack);
return $haystack;
}
http://php.net/manual/en/function.str-ireplace.php