note 85009 deleted from function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 14:47:26 +0000 |
| Subject: | note 85009 deleted from function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155680@lists.php.net to get a copy of this message | ||
Note Submitter: a dot lyskawa at pp-layouts dot com
----
It's obvious str_ireplace() won't be multi-byte safe. Function mb_str_ireplace() is
missing in php, so here's mine:
<?php
/**
* Case insensitive multi-byte safe str_ireplace
* @param string $search
* @param string $replace
* @param string $subject
* @param int $count
* @param string $encoding
* @return string
*/
function mb_str_ireplace($search, $replace, $subject, $count = null, $encoding = u8) {
$l1 = mb_strlen($search, $encoding);
$l2 = mb_strlen($replace, $encoding);
$rc = 0;
$offset = 0;
while(ok($p = mb_stripos($subject, $search, $offset, $encoding)) && (is_null($count) ||
$rc <= $count)) {
if (ok($p))
$subject = mb_substr($subject, 0, $p, $encoding) . $replace . mb_substr($subject, $p + $l1,
mb_strlen($subject, $encoding), $encoding);
$offset = $p + $l2;
$rc++;
}
return $subject;
}
?>
To make it mb_str_replace - change stripos to strpos. I'm sorry for previous bug, well, this
one is tested.
I wonder should I publish my growing mb_ functions library like WORKING mb_wordwrap WITHOUT regexes
(faster)... E-mail me.