note 87417 added to function.str-ireplace

From: Date: Thu, 04 Dec 2008 15:28:46 +0000
Subject: note 87417 added to function.str-ireplace
Groups: php.notes 
Request: Send a blank email to php-notes+get-147626@lists.php.net to get a copy of this message
Here's a different approach to search result keyword highlighting that will match all keyword sub strings in a case insensitive manner and preserve case in the returned text. This solution first grabs all matches within $haystack in a case insensitive manner, and the secondly loops through each of those matched sub strings and applies a case sensitive replace in $haystack. This way each unique (in terms of case) instance of $needle is operated on individually allowing a case sensitive replace to be done in order to preserve the original case of each unique instance of $needle. <?php function highlightStr($haystack, $needle, $highlightColorValue) { // return $haystack if there is no highlight color or strings given, nothing to do. if (strlen($highlightColorValue) < 1 || strlen($haystack) < 1 || strlen($needle) < 1) { return $haystack; } preg_match_all("/$needle+/i", $haystack, $matches); if (is_array($matches[0]) && count($matches[0]) >= 1) { foreach ($matches[0] as $match) { $haystack = str_replace($match, '<span style="background-color:'.$highlightColorValue.';">'.$match.'</span>', $haystack); } } return $haystack; } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 72.243.51.227 ---- Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php Edit -- https://master.php.net/note/edit/87417 Del: integrated -- https://master.php.net/note/delete/87417/integrated Del: useless -- https://master.php.net/note/delete/87417/useless Del: bad code -- https://master.php.net/note/delete/87417/bad+code Del: spam -- https://master.php.net/note/delete/87417/spam Del: non-english -- https://master.php.net/note/delete/87417/non-english Del: in docs -- https://master.php.net/note/delete/87417/in+docs Del: other reasons-- https://master.php.net/note/delete/87417 Reject -- https://master.php.net/note/reject/87417 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#147626) next »