-
-
Save bantya/563d7d070c286ba1b5a83b9036f0561a to your computer and use it in GitHub Desktop.
Multibyte substr_replace(). The mbstring library doesn’t come with a multibyte equivalent of substr_replace(). This function behaves exactly like substr_replace() even when the arguments are arrays.
This file contains hidden or 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
<?php | |
function mb_substr_replace($string, $replacement, $start, $length=NULL) { | |
if (is_array($string)) { | |
$num = count($string); | |
// $replacement | |
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement); | |
// $start | |
if (is_array($start)) { | |
$start = array_slice($start, 0, $num); | |
foreach ($start as $key => $value) | |
$start[$key] = is_int($value) ? $value : 0; | |
} | |
else { | |
$start = array_pad(array($start), $num, $start); | |
} | |
// $length | |
if (!isset($length)) { | |
$length = array_fill(0, $num, 0); | |
} | |
elseif (is_array($length)) { | |
$length = array_slice($length, 0, $num); | |
foreach ($length as $key => $value) | |
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0; | |
} | |
else { | |
$length = array_pad(array($length), $num, $length); | |
} | |
// Recursive call | |
return array_map(__FUNCTION__, $string, $replacement, $start, $length); | |
} | |
preg_match_all('/./us', (string)$string, $smatches); | |
preg_match_all('/./us', (string)$replacement, $rmatches); | |
if ($length === NULL) $length = mb_strlen($string); | |
array_splice($smatches[0], $start, $length, $rmatches[0]); | |
return join($smatches[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment