Skip to content

[12.x] Add ability to deduplicate multiple characters with a single Str::deduplicate call #56548

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

Merged
merged 4 commits into from
Aug 5, 2025

Conversation

WendellAdriel
Copy link
Contributor

Currently, if we want to deduplicate multiple characters at once from a string, we need to call the Str::deduplicate method multiple times.

$string = 'Laravelll    4ever';
$result = Str::deduplicate(Str::deduplicate($string, 'l')); // Laravel 4ever

With this change, we can do it in a single call, making it cleaner and easier to get the expected result:

$string = 'Laravelll    4ever';
$result = Str::deduplicate($string, [' ', 'l']); // Laravel 4ever

@shaedrich
Copy link
Contributor

shaedrich commented Aug 5, 2025

What's the real world use case here?

$string = 'Laravelll llm    4ever';
$result = Str::deduplicate(Str::deduplicate($string, 'l')); // Laravel lm 4ever (err, no, that's not what we want)

@WendellAdriel
Copy link
Contributor Author

What's the real world use case here?

$string = 'Laravelll llm    4ever';
$result = Str::deduplicate(Str::deduplicate($string, 'l')); // Laravel lm 4ever (err, no, that's not what we want)

The idea is not to have to call it multiple times.
The behaviour that you shared it's not an issue of the implementation of the multiple characters, it's how the deduplicate method works in general. If you call it like this, you'll get the same behaviour

$string = 'Laravell llm 4ever';
$result = Str::deduplicate($string, 'l'); // Laravel lm 4ever

@shaedrich
Copy link
Contributor

shaedrich commented Aug 5, 2025

Yeah, sorry, I just copy-pasted it. It wasn't about multiple calls. But the question remains.

@WendellAdriel
Copy link
Contributor Author

Imagine you're creating a feature that "improves" or "fixes" text that the user wrote, you can probably need to deduplicate all spaces and all signs of punctuation.

$formatted = Str::deduplicate($text, [' ', '.', ',', '!', '?']);

@shaedrich
Copy link
Contributor

Imagine you're creating a feature that "improves" or "fixes" text that the user wrote, you can probably need to deduplicate all spaces and all signs of punctuation.

$formatted = Str::deduplicate($text, [' ', '.', ',', '!', '?']);
$text = 'Loading...';
$formatted = Str::deduplicate($text, [' ', '.', ',', '!', '?']); // "Loading." (err, not quite)

@taylorotwell taylorotwell merged commit fc4cef4 into laravel:12.x Aug 5, 2025
58 of 60 checks passed
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.

3 participants