-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Conversation
1.x 👀 |
.php
from make commands and introduce Str::chop*.php
from make commands and introduce Str::chop*
8c5e307
to
81484dd
Compare
.php
from make commands and introduce Str::chop*Str::chopStart
, Str::chopEnd
, and fix trimming .php
from make commands
An extra parameter that makes these methods case-insensitive might be useful too Also, my first instinct was that these new methods are identical to // Identical in this example:
Str::chopEnd('path/to/file.php', '.php') // path/to/file
Str::beforeLast('path/to/file.php', '.php') // path/to/file
// Not identical in this example:
Str::chopEnd('path/to/file.php.tar.gz', '.php') // path/to/file.php.tar.gz
Str::beforeLast('path/to/file.php.tar.gz', '.php') // path/to/file |
A few suggestions. First, issue a PR to fix the bug instead of having a bug fix and new feature in the same PR. That way a patch can be released sooner. Second, while I love For example: Str::of($url)->replaceStart(['https://', 'http://']); |
I didn’t like adding this behaviour to replaceEnd / repaceStart because PHP’s native Str::replaceStart(['https://', 'www.'], '', 'https://www.laravel.com']);
// laravel.com It also has addditional behavior when the haystack is an array, which would have to be something else to consider. The static methods also have the replacement as the middle parameter, unlike the class based API. I felt a more specific and dedicated method was clearer. Edit: After chatting with you, I've removed the generator command fix and will leave that for you revert. |
Str::chopStart
, Str::chopEnd
, and fix trimming .php
from make commandsStr::chopStart
and Str::chopEnd
This PR introduces a two new string helper functions.
The "chop" methods will only ever chop once and only if the given string exists at the right end of the haystack:
Similar functionality can be achieved with
replaceEnd
andreplaceStart
:However, in the case of normalising URLs it becomes more verbose:
I feel this start / end normalization pattern is a common enough pattern that a dedicated method is a nice affordance, even with the tools already at our disposal (those above and regex).