note 30587 modified in function.str-replace by aidan

From: Date: Sun, 27 Jun 2004 13:46:29 +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-71893@lists.php.net to get a copy of this message
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>"); --was-- 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>"); http://php.net/manual/en/function.str-replace.php

« previous php.notes (#71893) next »