Skip to content

Commit

Permalink
Introduces Str::chop* (#51910)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Jun 27, 2024
1 parent 7e43459 commit be01fa5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
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

0 comments on commit be01fa5

Please sign in to comment.