Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Str::afterLast() and Str::beforeLast() helpers #30507

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
afterLast and beforeLast helpers added to Str
* includes multibyte support
  • Loading branch information
patrickomeara committed Nov 4, 2019
commit d2c3d81cce1a9b44a6ed9676c2f7805584a103fc
37 changes: 35 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Str
protected static $uuidFactory;

/**
* Return the remainder of a string after a given value.
* Return the remainder of a string after the first occurrence of a given value.
*
* @param string $subject
* @param string $search
Expand All @@ -52,6 +52,18 @@ public static function after($subject, $search)
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}

/**
* Return the remainder of a string after the last occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function afterLast($subject, $search)
{
return $search === '' ? $subject : array_reverse(explode($search, $subject))[0];
}

/**
* Transliterate a UTF-8 value to ASCII.
*
Expand All @@ -75,7 +87,7 @@ public static function ascii($value, $language = 'en')
}

/**
* Get the portion of a string before a given value.
* Get the portion of a string before the first occurrence of a given value.
*
* @param string $subject
* @param string $search
Expand All @@ -86,6 +98,27 @@ public static function before($subject, $search)
return $search === '' ? $subject : explode($search, $subject)[0];
}

/**
* Get the portion of a string before the last occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function beforeLast($subject, $search)
{
if ($search === '') {
return $subject;
}

$rpos = mb_strrpos($subject, $search);
if ($rpos === false) {
return $subject;
}

return static::substr($subject, 0, $rpos);
}

/**
* Convert a value to camel case.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ public function testStrBefore()
$this->assertSame('han', Str::before('han2nah', 2));
}

public function testStrBeforeLast()
{
$this->assertSame('yve', Str::beforeLast('yvette', 'tte'));
$this->assertSame('yvet', Str::beforeLast('yvette', 't'));
$this->assertSame('ééé ', Str::beforeLast('ééé yvette', 'yve'));
$this->assertSame('', Str::beforeLast('yvette', 'yve'));
$this->assertSame('yvette', Str::beforeLast('yvette', 'xxxx'));
$this->assertSame('yvette', Str::beforeLast('yvette', ''));
$this->assertSame('yv0et', Str::beforeLast('yv0et0te', '0'));
$this->assertSame('yv0et', Str::beforeLast('yv0et0te', 0));
$this->assertSame('yv2et', Str::beforeLast('yv2et2te', 2));
}

public function testStrAfter()
{
$this->assertSame('nah', Str::after('hannah', 'han'));
Expand All @@ -118,6 +131,19 @@ public function testStrAfter()
$this->assertSame('nah', Str::after('han2nah', 2));
}

public function testStrAfterLast()
{
$this->assertSame('tte', Str::afterLast('yvette', 'yve'));
$this->assertSame('e', Str::afterLast('yvette', 't'));
$this->assertSame('e', Str::afterLast('ééé yvette', 't'));
$this->assertSame('', Str::afterLast('yvette', 'tte'));
$this->assertSame('yvette', Str::afterLast('yvette', 'xxxx'));
$this->assertSame('yvette', Str::afterLast('yvette', ''));
$this->assertSame('te', Str::afterLast('yv0et0te', '0'));
$this->assertSame('te', Str::afterLast('yv0et0te', 0));
$this->assertSame('te', Str::afterLast('yv2et2te', 2));
}

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