Skip to content

Commit 617d03c

Browse files
authored
Make Str::endsWith return false if both haystack and needle are empty strings (#33434)
1 parent 6f3aa1f commit 617d03c

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/Illuminate/Support/Str.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static function containsAll($haystack, array $needles)
210210
public static function endsWith($haystack, $needles)
211211
{
212212
foreach ((array) $needles as $needle) {
213-
if (substr($haystack, -strlen($needle)) === (string) $needle) {
213+
if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) {
214214
return true;
215215
}
216216
}

tests/Support/SupportStrTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testStartsWith()
6060
$this->assertTrue(Str::startsWith('0123', 0));
6161
$this->assertFalse(Str::startsWith('jason', 'J'));
6262
$this->assertFalse(Str::startsWith('jason', ''));
63+
$this->assertFalse(Str::startsWith('', ''));
6364
$this->assertFalse(Str::startsWith('7', ' 7'));
6465
$this->assertTrue(Str::startsWith('7a', '7'));
6566
$this->assertTrue(Str::startsWith('7a', 7));
@@ -87,6 +88,7 @@ public function testEndsWith()
8788
$this->assertFalse(Str::endsWith('jason', 'no'));
8889
$this->assertFalse(Str::endsWith('jason', ['no']));
8990
$this->assertFalse(Str::endsWith('jason', ''));
91+
$this->assertFalse(Str::endsWith('', ''));
9092
$this->assertFalse(Str::endsWith('jason', [null]));
9193
$this->assertFalse(Str::endsWith('jason', null));
9294
$this->assertFalse(Str::endsWith('jason', 'N'));
@@ -183,6 +185,7 @@ public function testStrContains()
183185
$this->assertFalse(Str::contains('taylor', 'xxx'));
184186
$this->assertFalse(Str::contains('taylor', ['xxx']));
185187
$this->assertFalse(Str::contains('taylor', ''));
188+
$this->assertFalse(Str::contains('', ''));
186189
}
187190

188191
public function testStrContainsAll()

0 commit comments

Comments
 (0)