Created
December 6, 2013 18:30
-
-
Save fullpipe/7829842 to your computer and use it in GitHub Desktop.
php: mb_replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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