Skip to content

Commit b505ba4

Browse files
vlakofftaylorotwell
authored andcommitted
Suppress error if calling Str::replaceFirst with an empty search (#19427)
1 parent 5a5a4c8 commit b505ba4

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/Illuminate/Support/Str.php

+4
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ public static function replaceArray($search, array $replace, $subject)
321321
*/
322322
public static function replaceFirst($search, $replace, $subject)
323323
{
324+
if ($search == '') {
325+
return $subject;
326+
}
327+
324328
$position = strpos($subject, $search);
325329

326330
if ($position !== false) {

tests/Support/SupportStrTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public function testReplaceFirst()
190190
$this->assertEquals('foo/qux? foo/bar?', Str::replaceFirst('bar?', 'qux?', 'foo/bar? foo/bar?'));
191191
$this->assertEquals('foo foobar', Str::replaceFirst('bar', '', 'foobar foobar'));
192192
$this->assertEquals('foobar foobar', Str::replaceFirst('xxx', 'yyy', 'foobar foobar'));
193+
$this->assertEquals('foobar foobar', Str::replaceFirst('', 'yyy', 'foobar foobar'));
193194
}
194195

195196
public function testReplaceLast()
@@ -198,6 +199,7 @@ public function testReplaceLast()
198199
$this->assertEquals('foo/bar? foo/qux?', Str::replaceLast('bar?', 'qux?', 'foo/bar? foo/bar?'));
199200
$this->assertEquals('foobar foo', Str::replaceLast('bar', '', 'foobar foobar'));
200201
$this->assertEquals('foobar foobar', Str::replaceLast('xxx', 'yyy', 'foobar foobar'));
202+
$this->assertEquals('foobar foobar', Str::replaceLast('', 'yyy', 'foobar foobar'));
201203
}
202204

203205
public function testSnake()

0 commit comments

Comments
 (0)