note 30586 deleted from function.str-replace by aidan
| From: | aidan@php.net | Date: | Sun, 27 Jun 2004 13:46:22 +0000 |
| Subject: | note 30586 deleted from function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71892@lists.php.net to get a copy of this message | ||
Note Submitter: chirag@chime.tv
----
All the Google-like highlight functions above mess up when the needle is within the url too.
getHTMLHighlight function below works fine:
<?php
function getHighlight($needle, $haystack)
{
return preg_replace('|\b('.quotemeta($needle).')\b|iU', "<b
style=\"background-color:#FE923A\">".'\\1'."</b>",
$haystack);
}
function getHTMLHighlight($needle, $haystack)
{
$parts = explode(">", $haystack);
foreach($parts as $key=>$part)
{
if($pos = (strpos($part, "<")) === false)
$parts[$key] = getHighlight($needle, $part);
else
{
$partLeft = substr($part, 0, $pos-1);
$partRight = substr($part, $pos, strlen($part));
$parts[$key] = getHighlight($needle, $part) . $partRight;
}
}
return(implode(">", $parts));
}
?>