note 107631 modified in ref.mbstring by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 20:27:24 +0000 |
| Subject: | note 107631 modified in ref.mbstring by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187808@lists.php.net to get a copy of this message | ||
Looks like mb_str_replace is the most requested missing function from the multibyte string
library. I wanted a version of mb_str_replace with as similar a
code signature and behavior to str_replace as possible while conforming to the code signature
patterns of the mb library and avoiding performance pitfalls like unnecessary concatenations and
regular
expressions. <?php /**
* Multibyte safe version of str_replace. * See http://php.net/manual/en/function.str-replace.php
*/ function mb_str_replace( $search,
$replace, $subject, string $encoding = null,
int &$count = null) { if (is_array($subject))
{ $result = array(); foreach ($subject as $item)
{ $result[] = mb_str_replace($search, $replace, $item, $encoding,
$count); } return $result;
} if (!is_array($search)) { return
_mb_str_replace($search, $replace, $subject, $encoding, $count);
} $replace_is_array = is_array($replace);
foreach ($search as $key => $value) { $subject =
_mb_str_replace( $value, $replace_is_array ?
$replace[$key] : $replace, $subject,
$encoding, $count );
} return
$subject; } /** *
Implementation of mb_str_replace. Do not call directly. Enforces string
parameters. */ function _mb_str_replace(
string $search, string $replace, string
$subject, string $encoding = null, int &$count = null)
{ $search_length = mb_strlen($search,
$encoding); $subject_length = mb_strlen($subject, $encoding);
$offset = 0; $result = '';
while ($offset < $subject_length) { $match = mb_strpos($subject,
$search, $offset, $encoding); if ($match === false) { if
($offset === 0) { // No match was ever found, just return the
subject. return $subject; }
// Append the final portion of the subject to the replaced. $result
.= mb_substr($subject, $offset, $subject_length - $offset,
$encoding); break; } if ($count !==
null) { $count++; } $result .=
mb_substr($subject, $offset, $match - $offset, $encoding); $result .=
$replace; $offset = $match + $search_length;
} return
$result; } ?>
--was--
Looks like mb_str_replace is the most requested missing function from the multibyte string library.
I wanted a version of mb_str_replace with as similar a code signature and behavior to str_replace as
possible while conforming to the code signature patterns of the mb library and avoiding performance
pitfalls like unnecessary concatenations and regular expressions.
It's more LOC than others in this documentation thread, but it's also more complete.
/**
* Multibyte safe version of str_replace.
* See http://php.net/manual/en/function.str-replace.php
*/
function mb_str_replace(
$search,
$replace,
$subject,
string $encoding = null,
int &$count = null) {
if (is_array($subject)) {
$result = array();
foreach ($subject as $item) {
$result[] = mb_str_replace($search, $replace, $item, $encoding, $count);
}
return $result;
}
if (!is_array($search)) {
return _mb_str_replace($search, $replace, $subject, $encoding, $count);
}
$replace_is_array = is_array($replace);
foreach ($search as $key => $value) {
$subject = _mb_str_replace(
$value,
$replace_is_array ? $replace[$key] : $replace,
$subject,
$encoding,
$count
);
}
return $subject;
}
/**
* Implementation of mb_str_replace. Do not call directly. Enforces string parameters.
*/
function _mb_str_replace(
string $search,
string $replace,
string $subject,
string $encoding = null,
int &$count = null) {
$search_length = mb_strlen($search, $encoding);
$subject_length = mb_strlen($subject, $encoding);
$offset = 0;
$result = '';
while ($offset < $subject_length) {
$match = mb_strpos($subject, $search, $offset, $encoding);
if ($match === false) {
if ($offset === 0) {
// No match was ever found, just return the subject.
return $subject;
}
// Append the final portion of the subject to the replaced.
$result .=
mb_substr($subject, $offset, $subject_length - $offset, $encoding);
break;
}
if ($count !== null) {
$count++;
}
$result .= mb_substr($subject, $offset, $match - $offset, $encoding);
$result .= $replace;
$offset = $match + $search_length;
}
return $result;
}
http://php.net/manual/en/ref.mbstring.php