note 50073 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:24:26 +0000 |
| Subject: | note 50073 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114335@lists.php.net to get a copy of this message | ||
Note Submitter: spam at barad-dur dot nl
----
When you want to add searh highlights to your website you might find this useful.
<?php
function highlight($buff,$query) {
$query = explode($query,"+");
$class = 0;
foreach($query as $word) {
$buff = highlight_word($buff,$word,$class);
$class++;
}
return $buff;
}
function highlight_word($buff,$query,$class) {
$pattern = "/>(.*?)</";
$pattern_sub = "/($query)/i";
$ret = preg_replace_callback($pattern,
create_function(
'$matches',
'return preg_replace("'.$pattern_sub.'","<span
class=\"qr-'.$class.'\">$0</span>",$matches[0]);'
),
$buff;
return $ret;
}
?>
Where $buff is the content of your site and $query is the query via $_REQUEST (+ is an url-encoded
space)