Skip to content

Instantly share code, notes, and snippets.

@JBlond
Forked from stemar/mb_substr_replace.php
Last active January 31, 2024 06:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBlond/942f17f629f22e810fe3 to your computer and use it in GitHub Desktop.
Save JBlond/942f17f629f22e810fe3 to your computer and use it in GitHub Desktop.
<?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]);
}
@vldmrtmsk
Copy link

Super. By VITO

@itbro
Copy link

itbro commented Mar 29, 2019

Thank you!

@ttodua
Copy link

ttodua commented Nov 27, 2019

I used another variation ( https://pastebin.com/YBxpMXMv ) but the top one should be better.

@JBlond
Copy link
Author

JBlond commented Nov 28, 2019

@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