for my autosuggest functionality I need to colorize the excerpts in suggestions...so..
if I search something like "sa" the suggester gives me thefolowing
Samsung satire sata e-sata ...
Now what I'd like from my function is that it highlights - wraps the "sa" needle (all occuracies) qith a desired html tag.
like Samsung satire sata e-sata
- It should be utf8 safe
- It should preserve the capitalization
- it should ignore HTML
On the PHP site I found a function that does the replacement and keeps the capitalization... I modified it so it's utf8 friendly...
function ext_str_ireplace($findme, $replacewith, $text) {
// Replaces $findme in $subject with $replacewith
// Ignores the case and do keep the original capitalization by using $1 in $replacewith
// Required: PHP 5
$rest = $text;
$result = '';
while (mb_stripos($rest, $findme) !== false) {
$pos = mb_stripos($rest, $findme);
// Remove the wanted string from $rest and append it to $result
$result .= mb_substr($rest, 0, $pos);
$rest = mb_substr($rest, $pos, mb_strlen($rest)-$pos);
// Remove the wanted string from $rest and place it correctly into $result
$result .= mb_ereg_replace('$1', mb_substr($rest, 0, mb_strlen($findme)), $replacewith);
$rest = mb_substr($rest, mb_strlen($findme), mb_strlen($rest)-mb_strlen($findme));
}
// After the last match, append the rest
$result .= $rest;
return $result;
}
It works great...like
ext_str_ireplace("sa", "<b>sa</b>", "Samsung");
untill I pass some html code into it
$text= '<p class="red">A client is an application or system that accesses a service made available by a server. </p>';
ext_str_ireplace("cl", "<b>cl</b>", $text);
of course the function will replace the "cl" string in "