-
-
Save JBlond/942f17f629f22e810fe3 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* @param mixed $string The input string. | |
* @param mixed $replacement The replacement string. | |
* @param mixed $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. | |
* @param mixed $length If given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, it represents the number of characters from the end of string at which to stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. Of course, if length is zero then this function will have the effect of inserting replacement into string at the given start offset. | |
* @return string The result string is returned. If string is an array then array is returned. | |
*/ | |
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]); | |
} |
I used another variation ( https://pastebin.com/YBxpMXMv ) but the top one should be better.
@ttodua Yours requires mb string to be loaded
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!