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

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Jun 26, 2024

This PR introduces a two new string helper functions.

Str::chopEnd('path/to/file.php', '.php');

// "path/to/file"

Str::chopStart('https://laravel.com', ['https://', 'http://']);

// laravel.com

The "chop" methods will only ever chop once and only if the given string exists at the right end of the haystack:

Str::chopStart('https://www.laravel.com', ['https://', 'www.']);

// www.laravel.com

Similar functionality can be achieved with replaceEnd and replaceStart:

Str::replaceEnd('.php', '', 'path/to/file.php');

However, in the case of normalising URLs it becomes more verbose:

Str::of($url)->replaceStart('https://', '')->replaceStart('http://', '');

// compared to...

Str::chopStart($url, ['https://', 'http://']);

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).

@MrPunyapal
Copy link
Contributor

1.x 👀

@timacdonald timacdonald changed the title [1.x] Fix trimming .php from make commands and introduce Str::chop* [11.x] Fix trimming .php from make commands and introduce Str::chop* Jun 26, 2024
@timacdonald timacdonald force-pushed the str-chop branch 4 times, most recently from 8c5e307 to 81484dd Compare June 26, 2024 01:53
@timacdonald timacdonald changed the title [11.x] Fix trimming .php from make commands and introduce Str::chop* [11.x] Introduce Str::chopStart, Str::chopEnd, and fix trimming .php from make commands Jun 26, 2024
@SjorsO
Copy link
Contributor

SjorsO commented Jun 26, 2024

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 Str::after() and Str::beforeLast(), but that isn't true:

// 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

@jasonmccreary
Copy link
Contributor

jasonmccreary commented Jun 26, 2024

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 chop* methods, is it possible to update the replace*methods to accept an array of searches and potentially default the replacement (empty string). To your points, this would achieve the same thing with existing methods.

For example:

Str::of($url)->replaceStart(['https://', 'http://']);

@timacdonald
Copy link
Member Author

timacdonald commented Jun 26, 2024

I didn’t like adding this behaviour to replaceEnd / repaceStart because PHP’s native str_replace accepts an array and replaces all of the things in the array. If replace* was to accept an array I would think it would work in the same way, e.g.,

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.

@timacdonald timacdonald marked this pull request as draft June 26, 2024 22:42
@timacdonald timacdonald changed the title [11.x] Introduce Str::chopStart, Str::chopEnd, and fix trimming .php from make commands [11.x] Introduce Str::chopStart and Str::chopEnd Jun 26, 2024
@timacdonald timacdonald marked this pull request as ready for review June 26, 2024 23:06
@taylorotwell taylorotwell merged commit be01fa5 into laravel:11.x Jun 27, 2024
28 checks passed
@timacdonald timacdonald deleted the str-chop branch June 27, 2024 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants