note 30587 deleted from function.str-replace by felipe

From: Date: Sun, 23 Mar 2008 23:03:45 +0000
Subject: note 30587 deleted from function.str-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137416@lists.php.net to get a copy of this message
Note Submitter: chirag at chime dot 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 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>");

« previous php.notes (#137416) next »