note 51863 deleted from function.preg-replace by didou
| From: | didou@php.net | Date: | Sun, 23 Apr 2006 22:52:06 +0000 |
| Subject: | note 51863 deleted from function.preg-replace by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109231@lists.php.net to get a copy of this message | ||
Note Submitter:
----
If you want to highlight certain words with a different background color in your HTML output, then
this function will do the job. This is very useful when displaying search results etc..
<?PHP
// Function highlights $words in $str with the specified $color
function highlight_words($str, $words, $color = '#FFFF00') {
if(is_array($words)) {
foreach($words as $k => $word) {
$pattern[$k] = "/\b($word)\b/is";
$replace[$k] = '<font
style="background-color:'.$color.';">\\1</font>';
}
}
else {
$pattern = "/\b($words)\b/is";
$replace = '<font
style="background-color:'.$color.';">\\1</font>';
}
return preg_replace($pattern,$replace,$str);
}
?>
Hokus