note 30586 modified in function.str-replace by aidan

From: Date: Fri, 25 Jun 2004 06:07:22 +0000
Subject: note 30586 modified in function.str-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71643@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 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)); } ?> --was-- All the Google-like highlight functions above mess up when the needle is within the url too. getHTMLHighlight function below works fine: 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)); } http://php.net/manual/en/function.str-replace.php

« previous php.notes (#71643) next »