note 107313 deleted from function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:27:17 +0000 |
| Subject: | note 107313 deleted from function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187891@lists.php.net to get a copy of this message | ||
Note Submitter: victor at viathinksoft dot de
----
Here is an improved version of chud37.com's function. It now works with perfect matches, too -
highlight('abc', 'abc') didn't work in the old version.
<?php
function highlight($haystack, $needle) {
if (strlen($haystack) < 1 || strlen($needle) < 1) {return $haystack;}
preg_match_all("/$needle+/i", $haystack, $match);
$exploded = preg_split("/$needle+/i",$haystack);
$replaced = "";
foreach($exploded as $e) {
foreach($match as $m)
if ($i < count($exploded)-1) {$replaced .= $e . "<font
class=\"hl\">" . $m[0] . "</font>";} else {$replaced .= $e;}
$i++;
}
return $replaced;
}
?>