From b7967cbb988809956f1da15f293c780bcd6a0f46 Mon Sep 17 00:00:00 2001 From: Nasim Uddin <64758014+Nasim25@users.noreply.github.com> Date: Thu, 27 Nov 2025 01:14:33 +0600 Subject: [PATCH 1/3] Fix substrReplace to be multibyte safe --- src/Illuminate/Support/Str.php | 4 +++- tests/Support/SupportStrTest.php | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 164018788683..8c2135d52315 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1751,7 +1751,9 @@ public static function substrReplace($string, $replace, $offset = 0, $length = n $length = strlen($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)); From cd3ad36e74b3f0d11d8360d5ec95f62673c9681c Mon Sep 17 00:00:00 2001 From: Nasim Uddin <64758014+Nasim25@users.noreply.github.com> Date: Thu, 27 Nov 2025 02:32:13 +0600 Subject: [PATCH 2/3] Apply code style fixes --- src/Illuminate/Support/Str.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 8c2135d52315..bdfab9d27fae 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1752,8 +1752,8 @@ public static function substrReplace($string, $replace, $offset = 0, $length = n } return mb_substr($string, 0, $offset) - . $replace - . mb_substr($string, $offset + $length); + .$replace + .mb_substr($string, $offset + $length); } /** From 4bfb0b7e159777ed261f9f4a6615904dd68b78d1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 27 Nov 2025 10:35:33 -0600 Subject: [PATCH 3/3] formatting --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index bdfab9d27fae..664d445f493b 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1748,7 +1748,7 @@ 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 mb_substr($string, 0, $offset)