Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ class Str
*/
protected static $studlyCache = [];

/**
* Return the remainder of a string after a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function after($subject, $search)
{
if (! static::contains($subject, $search)) {
return $subject;
}

$searchEndPos = strpos($subject, $search) + static::length($search);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strpos is single-byte while the rest of the code is utf8 aware.
Therefore you get wrong results with after('é foobarbaz', 'foo')


return static::substr($subject, $searchEndPos, static::length($subject));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We grab up to the end, so the third parameter should be omitted.
(as more, here it exceeds the end the subject; it works but is erroneous)

}

/**
* Transliterate a UTF-8 value to ASCII.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,20 @@ function starts_with($haystack, $needles)
}
}

if (! function_exists('str_after')) {
/**
* Return the remainder of a string after a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
function str_after($subject, $search)
{
return Str::after($subject, $search);
}
}

if (! function_exists('str_contains')) {
/**
* Determine if a given string contains a given substring.
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ public function testEndsWith()
$this->assertFalse(Str::endsWith('jason', ['no']));
}

public function testStrAfter()
{
$this->assertEquals('nah', str_after('hannah', 'han'));
$this->assertEquals('nah', str_after('hannah', 'n'));
$this->assertEquals('hannah', str_after('hannah', 'xxxx'));
}

public function testStrContains()
{
$this->assertTrue(Str::contains('taylor', 'ylo'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function testEndsWith()
$this->assertFalse(Str::endsWith(0.27, '8'));
}

public function testStrAfter()
{
$this->assertEquals('nah', Str::after('hannah', 'han'));
$this->assertEquals('nah', Str::after('hannah', 'n'));
$this->assertEquals('hannah', Str::after('hannah', 'xxxx'));
}

public function testStrContains()
{
$this->assertTrue(Str::contains('taylor', 'ylo'));
Expand Down