note 81539 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:08:05 +0000 |
| Subject: | note 81539 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137436@lists.php.net to get a copy of this message | ||
Note Submitter: ash at atomic-network dot co dot uk
----
/***
*
* Highlight specified text.
* @param keyword which you want highlighting
* @param the bulk of text your highlighting keywords from.
*
* This example is very basic, and explodes the search words by a space, into an array.
* Nonetheless one worded searches are also placed into a single array.
*/
function QuickHighlight($searchstr,$str){
$search =((strrpos($searchstr," ") ===false)?array($searchstr):explode("
",$searchstr));
$content =$str;
foreach($search as $keyword) $content =str_ireplace($keyword,"<span
style='background-color:yellow;'>$keyword</span>", $content);
return $content;
}
Note:
str_ireplace is quicker than ereg and there isn't any regular expressions used.
for php 4 users, you will need to use str_replace, and play around with some
case sencitivity.