diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 164018788683..664d445f493b 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1748,10 +1748,12 @@ public static function substrCount($haystack, $needle, $offset = 0, $length = nu public static function substrReplace($string, $replace, $offset = 0, $length = null) { if ($length === null) { - $length = strlen($string); + $length = static::length($string); } - return substr_replace($string, $replace, $offset, $length); + return mb_substr($string, 0, $offset) + .$replace + .mb_substr($string, $offset + $length); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index a29252a0bff3..478f65b81269 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -1275,6 +1275,12 @@ public function testSubstrReplace() $this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8)); } + public function testSubstrReplaceWithMultibyte() + { + $this->assertSame('kengä', Str::substrReplace('kenkä', 'ng', -3, 2)); + $this->assertSame('kenga', Str::substrReplace('kenka', 'ng', -3, 2)); + } + public function testTake() { $this->assertSame('ab', Str::take('abcdef', 2));