diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index 83e2bf858..62dcf4e96 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -1,5 +1,9 @@ # v3.0.40 - TBD +## Fixed + +- [#6217](https://github.com/hyperf/hyperf/pull/6217) Fixed bug that `Str::replaceLast` with empty search cannot work as expected. + ## Optimized - [#6209](https://github.com/hyperf/hyperf/pull/6209) Support for phpredis 6.x [sentinel](https://github.com/phpredis/phpredis/blob/develop/sentinel.md#examples-for-version-60-or-later) diff --git a/src/stringable/src/Str.php b/src/stringable/src/Str.php index b9fd9ca5e..c62eb4e6e 100644 --- a/src/stringable/src/Str.php +++ b/src/stringable/src/Str.php @@ -601,6 +601,10 @@ class Str */ public static function replaceLast(string $search, string $replace, string $subject): string { + if ($search == '') { + return $subject; + } + $position = strrpos($subject, $search); if ($position !== false) { diff --git a/src/stringable/tests/StrTest.php b/src/stringable/tests/StrTest.php index 72270768e..1dea2edd9 100644 --- a/src/stringable/tests/StrTest.php +++ b/src/stringable/tests/StrTest.php @@ -558,4 +558,10 @@ class StrTest extends TestCase $this->assertSame($item[0], Str::convertCase(...$item[1])); } } + + public function testReplaceLast() + { + $this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world')); + $this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world')); + } }