note 27741 modified in function.str-replace by aidan
| From: | aidan@php.net | Date: | Sat, 26 Jun 2004 14:02:58 +0000 |
| Subject: | note 27741 modified in function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71773@lists.php.net to get a copy of this message | ||
To do a 'Google' alike highligh with keeping the original case (improved version):
<?php
// highlights words in text (default like Google, set $hs and $he to change)
function highlight( $words, $haystack, $hs = '<b style="color: black; background-color:
#ffff66">', $he = '</b>'){
if (is_array( $words))
foreach( $words as $word)
$haystack = preg_replace('|\b('.quotemeta($word).')\b|iU',
$hs.'\\1'.$he, $haystack);
else
$haystack = preg_replace('|\b('.quotemeta($words).')\b|iU',
$hs.'\\1'.$he, $haystack);
return $haystack;
}
?>
--was--
To do a 'Google' alike highligh with keeping the original case (improved version):
// highlights words in text (default like Google, set $hs and $he to change)
function highlight( $words, $haystack, $hs = '<b style="color: black; background-color:
#ffff66">', $he = '</b>'){
if (is_array( $words))
foreach( $words as $word)
$haystack = preg_replace('|\b('.quotemeta($word).')\b|iU',
$hs.'\\1'.$he, $haystack);
else
$haystack = preg_replace('|\b('.quotemeta($words).')\b|iU',
$hs.'\\1'.$he, $haystack);
return $haystack;
}
http://php.net/manual/en/function.str-replace.php