note 30587 modified in function.str-replace by aidan
| From: | aidan@php.net | Date: | Fri, 25 Jun 2004 06:07:08 +0000 |
| Subject: | note 30587 modified in function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71642@lists.php.net to get a copy of this message | ||
Rather than use my above two functions, please use the following function:
<?php
function getHTMLHighlight($needle, $haystack, $hlS, $hlE)
{
$parts = explode(">", $haystack);
foreach($parts as $key=>$part)
{
$pL = "";
$pR = "";
if(($pos = strpos($part, "<")) === false)
$pL = $part;
elseif($pos > 0)
{
$pL = substr($part, 0, $pos);
$pR = substr($part, $pos, strlen($part));
}
if($pL != "")
$parts[$key] = preg_replace('|\b('.quotemeta($needle).')\b|iU',
$hlS.'\\1'.$hlE, $pL) . $pR;
}
return(implode(">", $parts));
}
?>
Usage:
getHTMLHighlight($needle, $haystack, "<b
style=\"background-color:#FF3145\">", "</b>");
--was--
Rather than use my above two functions, please use the following function:
function getHTMLHighlight($needle, $haystack, $hlS, $hlE)
{
$parts = explode(">", $haystack);
foreach($parts as $key=>$part)
{
$pL = "";
$pR = "";
if(($pos = strpos($part, "<")) === false)
$pL = $part;
elseif($pos > 0)
{
$pL = substr($part, 0, $pos);
$pR = substr($part, $pos, strlen($part));
}
if($pL != "")
$parts[$key] = preg_replace('|\b('.quotemeta($needle).')\b|iU',
$hlS.'\\1'.$hlE, $pL) . $pR;
}
return(implode(">", $parts));
}
Usage:
getHTMLHighlight($needle, $haystack, "<b
style=\"background-color:#FF3145\">", "</b>");
http://php.net/manual/en/function.str-replace.php