Skip to content

[9.x] add dictionary to slug helper#44730

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
imdhemy:add-dictionary-to-slug-helper
Oct 25, 2022
Merged

[9.x] add dictionary to slug helper#44730
taylorotwell merged 4 commits into
laravel:9.xfrom
imdhemy:add-dictionary-to-slug-helper

Conversation

@imdhemy
Copy link
Copy Markdown
Contributor

@imdhemy imdhemy commented Oct 25, 2022

The current implementation of the Str::slug() method already replaces the @ with at. The keyboard still has some other replaceable characters.

E.g 500$ bill becomes 500-dollar-bill instead of 500-bill.

Besides, some languages have different rules for the replacement of special characters.
With the current implementation, we can have something like:

أحمد-at-المدرسة

which should be:

أحمد-في-المدرسة

This PR adds a new parameter to the Str::slug() method to allow the developer to specify the replacement character for the special characters.

It does not break the current implementation, as the default value for the new parameter is ['@' => 'at'].

- allows custom replacements like `@` => `at`, `$` => `dollar`
Copy link
Copy Markdown
Contributor

@milwad-dev milwad-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is useful

@taylorotwell taylorotwell merged commit 9d4a5b3 into laravel:9.x Oct 25, 2022
@driesvints
Copy link
Copy Markdown
Member

@milwad-dev please only comment when you have something to contribute to the discussion.

@shaedrich
Copy link
Copy Markdown
Contributor

Wouldn't it be better to have a more diverse method signature like this:

Str::slug('500$ bill', [ '$' => fn($value) => /* do something */]);
Str::slug('500$ bill', fn($value) => /* do something */);
Str::slug('500$ bill', [ '/$|€/' => fn($value) => /* do something */]);

RegExp would even work quite well, since it is already used in that method.

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