note 107179 deleted from function.str-ireplace by tularis
| From: | tularis@php.net | Date: | Thu, 12 Jan 2012 15:30:53 +0000 |
| Subject: | note 107179 deleted from function.str-ireplace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185612@lists.php.net to get a copy of this message | ||
Note Submitter: chud37.com
Reason: bad code
----
Apolagies, there was an error which I neglected, it always replaced with the first needle.
Therefore, if you chose to highlight the letter 'A', and you have a capital A at the
beggining and a lower case 'a' futher on in the string, it would place capitals through
it. This revised version takes care of that.
<?php
function highlight($haystack, $needle) {
//echo "<br>$haystack--$needle<br>";
preg_match_all("/$needle/i", $haystack, $match);
$m = $match[0]; $replaced = "";
$exploded = preg_split("/$needle/i",$haystack);
foreach($exploded as $e)
if($e!=$exploded[count($exploded)-1]) {$replaced .= $e . "<font
style=\"background-color:yellow\">" . $m[array_search($e,$exploded)] .
"</font>";} else {$replaced .= $e;}
return $replaced;
}
?>