您可以使用这两个函数:
From shkspr.mobi https://shkspr.mobi/blog/2012/09/a-utf-8-aware-substr_replace-for-use-in-app-net/:
function mb_substr_replace($original, $replacement, $position, $length)
{
$startString = mb_substr($original, 0, $position, "UTF-8");
$endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
$out = $startString . $replacement . $endString;
return $out;
}
From GitHub https://github.com/fluxbb/utf8/blob/master/functions/substr_replace.php:
function mb_substr_replace($str, $repl, $start, $length = null)
{
preg_match_all('/./us', $str, $ar);
preg_match_all('/./us', $repl, $rar);
$length = is_int($length) ? $length : utf8_strlen($str);
array_splice($ar[0], $start, $length, $rar[0]);
return implode($ar[0]);
}
我都尝试过,都效果很好。