Skip to content

Commit 21e593d

Browse files
authored
Added Str::chopStart and Str::chopEnd (#6917)
1 parent ceb6417 commit 21e593d

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

β€Žsrc/Str.php

+36
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,42 @@ public static function charAt($subject, $index)
232232
return mb_substr($subject, $index, 1);
233233
}
234234

235+
/**
236+
* Remove the given string(s) if it exists at the end of the haystack.
237+
*
238+
* @param string $subject
239+
* @param array|string $needle
240+
* @return string
241+
*/
242+
public static function chopEnd($subject, $needle)
243+
{
244+
foreach ((array) $needle as $n) {
245+
if (str_ends_with($subject, $n)) {
246+
return substr($subject, 0, -strlen($n));
247+
}
248+
}
249+
250+
return $subject;
251+
}
252+
253+
/**
254+
* Remove the given string(s) if it exists at the start of the haystack.
255+
*
256+
* @param string $subject
257+
* @param array|string $needle
258+
* @return string
259+
*/
260+
public static function chopStart($subject, $needle)
261+
{
262+
foreach ((array) $needle as $n) {
263+
if (str_starts_with($subject, $n)) {
264+
return substr($subject, strlen($n));
265+
}
266+
}
267+
268+
return $subject;
269+
}
270+
235271
/**
236272
* Determine if a given string contains a given substring.
237273
*

β€Žsrc/Stringable.php

+22
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,28 @@ public function charAt($index)
141141
return Str::charAt($this->value, $index);
142142
}
143143

144+
/**
145+
* Remove the given string if it exists at the end of the current string.
146+
*
147+
* @param array|string $needle
148+
* @return static
149+
*/
150+
public function chopEnd($needle)
151+
{
152+
return new static(Str::chopEnd($this->value, $needle));
153+
}
154+
155+
/**
156+
* Remove the given string if it exists at the start of the current string.
157+
*
158+
* @param array|string $needle
159+
* @return static
160+
*/
161+
public function chopStart($needle)
162+
{
163+
return new static(Str::chopStart($this->value, $needle));
164+
}
165+
144166
/**
145167
* Get the basename of the class path.
146168
*

β€Žtests/StrTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -1131,4 +1131,44 @@ public function testFromBase64(): void
11311131
$this->assertSame('foo', Str::fromBase64(base64_encode('foo')));
11321132
$this->assertSame('foobar', Str::fromBase64(base64_encode('foobar'), true));
11331133
}
1134+
1135+
public function testChopStart()
1136+
{
1137+
foreach ([
1138+
'http://laravel.com' => ['http://', 'laravel.com'],
1139+
'http://-http://' => ['http://', '-http://'],
1140+
'http://laravel.com' => ['htp:/', 'http://laravel.com'],
1141+
'http://laravel.com' => ['http://www.', 'http://laravel.com'],
1142+
'http://laravel.com' => ['-http://', 'http://laravel.com'],
1143+
'http://laravel.com' => [['https://', 'http://'], 'laravel.com'],
1144+
'http://www.laravel.com' => [['http://', 'www.'], 'www.laravel.com'],
1145+
'http://http-is-fun.test' => ['http://', 'http-is-fun.test'],
1146+
'πŸŒŠβœ‹' => ['🌊', 'βœ‹'],
1147+
'πŸŒŠβœ‹' => ['βœ‹', 'πŸŒŠβœ‹'],
1148+
] as $subject => $value) {
1149+
[$needle, $expected] = $value;
1150+
1151+
$this->assertSame($expected, Str::chopStart($subject, $needle));
1152+
}
1153+
}
1154+
1155+
public function testChopEnd()
1156+
{
1157+
foreach ([
1158+
'path/to/file.php' => ['.php', 'path/to/file'],
1159+
'.php-.php' => ['.php', '.php-'],
1160+
'path/to/file.php' => ['.ph', 'path/to/file.php'],
1161+
'path/to/file.php' => ['foo.php', 'path/to/file.php'],
1162+
'path/to/file.php' => ['.php-', 'path/to/file.php'],
1163+
'path/to/file.php' => [['.html', '.php'], 'path/to/file'],
1164+
'path/to/file.php' => [['.php', 'file'], 'path/to/file'],
1165+
'path/to/php.php' => ['.php', 'path/to/php'],
1166+
'βœ‹πŸŒŠ' => ['🌊', 'βœ‹'],
1167+
'βœ‹πŸŒŠ' => ['βœ‹', 'βœ‹πŸŒŠ'],
1168+
] as $subject => $value) {
1169+
[$needle, $expected] = $value;
1170+
1171+
$this->assertSame($expected, Str::chopEnd($subject, $needle));
1172+
}
1173+
}
11341174
}

0 commit comments

Comments
Β (0)