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

[11.x] Introduce Str::chopStart and Str::chopEnd #51910

Merged
merged 1 commit into from
Jun 27, 2024
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
36 changes: 36 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,42 @@ public static function charAt($subject, $index)
return mb_substr($subject, $index, 1);
}

/**
* Remove the given string(s) if it exists at the end of the haystack.
*
* @param string $subject
* @param string|array $needle
* @return string
*/
public static function chopEnd($subject, $needle)
{
foreach ((array) $needle as $n) {
if (str_ends_with($subject, $n)) {
return substr($subject, 0, -strlen($n));
}
}

return $subject;
}

/**
* Remove the given string(s) if it exists at the start of the haystack.
*
* @param string $subject
* @param string|array $needle
* @return string
*/
public static function chopStart($subject, $needle)
{
foreach ((array) $needle as $n) {
if (str_starts_with($subject, $n)) {
return substr($subject, strlen($n));
}
}

return $subject;
}

/**
* Determine if a given string contains a given substring.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ public function charAt($index)
return Str::charAt($this->value, $index);
}

/**
* Remove the given string if it exists at the end of the current string.
*
* @param string|array $needle
* @return static
*/
public function chopEnd($needle)
{
return new static(Str::chopEnd($this->value, $needle));
}

/**
* Remove the given string if it exists at the start of the current string.
*
* @param string|array $needle
* @return static
*/
public function chopStart($needle)
{
return new static(Str::chopStart($this->value, $needle));
}

/**
* Get the basename of the class path.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,46 @@ public function testFromBase64()
$this->assertSame('foo', Str::fromBase64(base64_encode('foo')));
$this->assertSame('foobar', Str::fromBase64(base64_encode('foobar'), true));
}

public function testChopStart()
{
foreach ([
'http://laravel.com' => ['http://', 'laravel.com'],
'http://-http://' => ['http://', '-http://'],
'http://laravel.com' => ['htp:/', 'http://laravel.com'],
'http://laravel.com' => ['http://www.', 'http://laravel.com'],
'http://laravel.com' => ['-http://', 'http://laravel.com'],
'http://laravel.com' => [['https://', 'http://'], 'laravel.com'],
'http://www.laravel.com' => [['http://', 'www.'], 'www.laravel.com'],
'http://http-is-fun.test' => ['http://', 'http-is-fun.test'],
'🌊✋' => ['🌊', '✋'],
'🌊✋' => ['✋', '🌊✋'],
] as $subject => $value) {
[$needle, $expected] = $value;

$this->assertSame($expected, Str::chopStart($subject, $needle));
}
}

public function testChopEnd()
{
foreach ([
'path/to/file.php' => ['.php', 'path/to/file'],
'.php-.php' => ['.php', '.php-'],
'path/to/file.php' => ['.ph', 'path/to/file.php'],
'path/to/file.php' => ['foo.php', 'path/to/file.php'],
'path/to/file.php' => ['.php-', 'path/to/file.php'],
'path/to/file.php' => [['.html', '.php'], 'path/to/file'],
'path/to/file.php' => [['.php', 'file'], 'path/to/file'],
'path/to/php.php' => ['.php', 'path/to/php'],
'✋🌊' => ['🌊', '✋'],
'✋🌊' => ['✋', '✋🌊'],
] as $subject => $value) {
[$needle, $expected] = $value;

$this->assertSame($expected, Str::chopEnd($subject, $needle));
}
}
}

class StringableObjectStub
Expand Down