note 93338 deleted from function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Fri, 04 Sep 2009 18:06:04 +0000 |
| Subject: | note 93338 deleted from function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160219@lists.php.net to get a copy of this message | ||
Note Submitter: o dot heil at dkfz dot de
----
A more compact form of the highlighting problem with preserved capitalization:
<?php
//
//find and highlight all occurences of string $highlightme in string $subject
// - ignore case when searching
// - preserve original case when putting <span>...</span> around it
//example:
// print(highlight("cde","--AbCdEfG--"));
//will output:
// --Ab<span class=\"highlighted\">CdE</span>fG--
//
function highlight($highlightme, $subject)
{
$result = $subject;
preg_match_all("/(".$highlightme.")/i",$subject,$matches);
foreach( $matches[0] as $match ) {
$result = str_replace($match,"<span
class=\"highlighted\">".$match."</span>",$result);
}
return $result;
}
?>