Skip to content

Commit 4f1781c

Browse files
authored
Fixed bug that Str::replaceLast with empty search cannot work as expected. (#6217)
1 parent 63d7ce0 commit 4f1781c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Str.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ public static function replaceFirst(string $search, string $replace, string $sub
601601
*/
602602
public static function replaceLast(string $search, string $replace, string $subject): string
603603
{
604+
if ($search == '') {
605+
return $subject;
606+
}
607+
604608
$position = strrpos($subject, $search);
605609

606610
if ($position !== false) {

tests/StrTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,10 @@ public function testConvertCase()
558558
$this->assertSame($item[0], Str::convertCase(...$item[1]));
559559
}
560560
}
561+
562+
public function testReplaceLast()
563+
{
564+
$this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world'));
565+
$this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world'));
566+
}
561567
}

0 commit comments

Comments
 (0)