Skip to content

Instantly share code, notes, and snippets.

@fullpipe
Created December 6, 2013 18:30
Show Gist options
  • Save fullpipe/7829842 to your computer and use it in GitHub Desktop.
Save fullpipe/7829842 to your computer and use it in GitHub Desktop.
php: mb_replace
function mb_replace($search, $replace, $subject, &$count=0) {
if (!is_array($search) && is_array($replace)) {
return false;
}
if (is_array($subject)) {
// call mb_replace for each single string in $subject
foreach ($subject as &$string) {
$string = &mb_replace($search, $replace, $string, $c);
$count += $c;
}
} elseif (is_array($search)) {
if (!is_array($replace)) {
foreach ($search as &$string) {
$subject = mb_replace($string, $replace, $subject, $c);
$count += $c;
}
} else {
$n = max(count($search), count($replace));
while ($n--) {
$subject = mb_replace(current($search), current($replace), $subject, $c);
$count += $c;
next($search);
next($replace);
}
}
} else {
$parts = mb_split(preg_quote($search), $subject);
$count = count($parts)-1;
$subject = implode($replace, $parts);
}
return $subject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment