note 84917 added to function.str-ireplace
| From: | a dot lyskawa at pp-layouts dot com | Date: | Wed, 06 Aug 2008 00:07:36 +0000 |
| Subject: | note 84917 added to function.str-ireplace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143191@lists.php.net to get a copy of this message | ||
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_replace
* @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 = 'utf-8')
{
$l1 = mb_strlen($search, $encoding);
$rc = 0;
$offset = 0;
while (ok($p = mb_stripos($subject, $search, $offset, $encoding)) && (is_null($rc) || $rc
<= $count)) {
if (ok($p)) $subject = mb_substr($subject, $offset, $p, $encoding) . $replace .
mb_substr($subject, $p + $l1, mb_strlen($subject, $encoding), $encoding);
$offset = $p + $l1;
$rc++;
}
return $subject;
}
?>
It works exactly like original str_ireplace() and defaults to 'utf-8' encoding. Replace
with null if you like. Well, it's surely slower than built-in functions, however it's one
of the fastest ways to do the trick.
----
Server IP: 82.160.94.10
Probable Submitter: 89.76.123.253
----
Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php
Edit -- https://master.php.net/note/edit/84917
Del: integrated -- https://master.php.net/note/delete/84917/integrated
Del: useless -- https://master.php.net/note/delete/84917/useless
Del: bad code -- https://master.php.net/note/delete/84917/bad+code
Del: spam -- https://master.php.net/note/delete/84917/spam
Del: non-english -- https://master.php.net/note/delete/84917/non-english
Del: in docs -- https://master.php.net/note/delete/84917/in+docs
Del: other reasons-- https://master.php.net/note/delete/84917
Reject -- https://master.php.net/note/reject/84917
Search -- https://master.php.net/manage/user-notes.php