Skip to content

Commit

Permalink
Fixes and optimizations for Str::after (#19428)
Browse files Browse the repository at this point in the history
* Correct results if there are multibyte characters before the search
* Do not trigger warning if search is an empty string

Also, should be significantly faster.
  • Loading branch information
vlakoff authored and taylorotwell committed Jun 1, 2017
1 parent 67de1d6 commit 5a5a4c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ class Str
*/
public static function after($subject, $search)
{
if (! static::contains($subject, $search)) {
if ($search == '') {
return $subject;
}

$end = strpos($subject, $search) + static::length($search);
$pos = strpos($subject, $search);

return static::substr($subject, $end, static::length($subject));
if ($pos === false) {
return $subject;
}

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

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function testStrAfter()
{
$this->assertEquals('nah', Str::after('hannah', 'han'));
$this->assertEquals('nah', Str::after('hannah', 'n'));
$this->assertEquals('nah', Str::after('ééé hannah', 'han'));
$this->assertEquals('hannah', Str::after('hannah', 'xxxx'));
$this->assertEquals('hannah', Str::after('hannah', ''));
}

public function testStrContains()
Expand Down

0 comments on commit 5a5a4c8

Please sign in to comment.