note 50073 added to function.preg-replace
| From: | spam at barad-dur dot nl | Date: | Thu, 17 Feb 2005 09:21:21 +0000 |
| Subject: | note 50073 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85151@lists.php.net to get a copy of this message | ||
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)
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50073
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50073&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50073&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50073&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50073&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50073&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50073&report=yes
Search -- http://master.php.net/manage/user-notes.php