-
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
Weird behavior of Str::slug in particular language #54444
Comments
The first parameter is not the language, but the separator. You can use named parameters to specify a language while using the default separator:
|
@rodrigopedra But it does not explain how is |
This line here, on the framework/src/Illuminate/Support/Str.php Lines 1483 to 1484 in 53091c2
When the separator has more than one character, all of its characters are added to the regular expression's character class. So the expression on the line above will evaluate to: preg_replace('![az\s]+!u', 'az', 'nad') Which means it will replace any letters Thus, as You can try with this simple script: <?php
$title = 'nad';
$separator = 'az';
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
echo $title, \PHP_EOL; One could propose a PR to assert a separator only has a single character. On the other hand, people could be relying on the current behavior to generate strings like this:
|
Multiple characters might not be a problem, as long as they are proper delimiters 🤔 |
Oh, you're right. I don't remember when I last used this method, until now (wrote it once in |
Yeah, maybe if it worked like expected, without any replace, because the input doesn't have any spaces, it would help me to notice the source of the issue. |
By the way, please note that the method is only designed for single-character separators, mainly due to the regex character classes. I considered suggesting support for multi-character separators, but it would be somewhat difficult to implement because of the negated character class (here). There are various ways to solve this, but it would complicate the code, and I don't think it would be worth it. To add some robustness, we could throw an exception if the provided separator is longer than one character. |
I attempted this implementation of multi-character separators, and as expected, it made the code more complex, harder to understand, and more prone to regression. Therefore, I reaffirm that it is not worth pursuing. Just as a reminder, the second argument of the final |
Laravel Version
11.41.3
PHP Version
8.3.16
Database Driver & Version
No response
Description
I think it's better just to show:
First, I couldn't realize why slug becomes longer, then noticed extra letters. Then tried to manually delete letters to find out letter combinations that cause this issue.
Maybe it happens in other languages too, but I tested some languages, they all return
nad
.Maybe there is are other combinations also, that behaves weird, but I didn't test.
First, I thought that it's because of the
ascii
method, but no. I couldn't find the source of this issue.UPDATE
Find out a little bit more combinations (in the second example letter is trimmed):
Steps To Reproduce
Just do:
str('nad')->slug('az')
str('yab')->slug('az')
str('ab')->slug('az')
The text was updated successfully, but these errors were encountered: