Skip to content

Commit

Permalink
Fix Str::afterLast (#31095)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored and taylorotwell committed Jan 10, 2020
1 parent 4e33586 commit b9e33b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Illuminate/Support/Str.php
Expand Up @@ -61,7 +61,17 @@ public static function after($subject, $search)
*/
public static function afterLast($subject, $search)
{
return $search === '' ? $subject : array_reverse(explode($search, $subject))[0];
if ($search === '') {
return $subject;
}

$position = strrpos($subject, (string) $search);

if ($position === false) {
return $subject;

This comment has been minimized.

Copy link
@voku

voku Feb 28, 2020

Contributor

Is it wanted, that we get the full string, if the searched value is not in the subject?

Here are some tests from portable-utf8: https://github.com/voku/portable-utf8/blob/9ba13f6effba0a293ae1726b384fa8d9ced1c066/tests/Utf8TestsFromStringyTest.php#L1781

This comment has been minimized.

Copy link
@driesvints

driesvints Feb 28, 2020

Author Member

Yes, that's the current behavior.

This comment has been minimized.

Copy link
@voku

voku Feb 28, 2020

Contributor

Is that also the expected behavior?

This comment has been minimized.

Copy link
@driesvints

driesvints Mar 2, 2020

Author Member

Yes

Screenshot 2020-03-02 at 12 05 49

}

return substr($subject, $position + strlen($search));
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Support/SupportStrTest.php
Expand Up @@ -142,6 +142,7 @@ public function testStrAfterLast()
$this->assertSame('te', Str::afterLast('yv0et0te', '0'));
$this->assertSame('te', Str::afterLast('yv0et0te', 0));
$this->assertSame('te', Str::afterLast('yv2et2te', 2));
$this->assertSame('foo', Str::afterLast('----foo', '---'));
}

public function testStrContains()
Expand Down

0 comments on commit b9e33b8

Please sign in to comment.