-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Made the slugger compatible with every language #389
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
Conversation
@@ -27,6 +27,6 @@ class Slugger | |||
*/ | |||
public function slugify($string) | |||
{ | |||
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-'); | |||
return str_replace(' ', '-', strtolower(trim(strip_tags($string)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should replace any whitespace char, not only spaces. And you should not put several dashes for consecutive spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will /\s+/
regex pattern be enough here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bocharsky-bw yes, I did that at the end 👍
@@ -27,6 +27,6 @@ class Slugger | |||
*/ | |||
public function slugify($string) | |||
{ | |||
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-'); | |||
return str_replace(' ', '-', strtolower(trim(strip_tags($string)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, if you want to support any language, you should use mb_strtolower
There are some failing tests only for PHP 5.5. I don't understand why because we use Symfony's polyfill for mb_strtolower():
|
@javiereguiluz, Could we use the slugs without changing their cases? |
@javiereguiluz, I think it doesn't work because you use unsupported symbols like |
@voronkovich I took that from the Wikipedia 😱 Is it wrong? |
@@ -27,6 +27,6 @@ class Slugger | |||
*/ | |||
public function slugify($string) | |||
{ | |||
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-'); | |||
return preg_replace('/\s+/', '-', mb_strtolower(trim(strip_tags($string)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should force the encoding to UTF-8 for mb_strtolower. The internal encoding of mbstring might be configured to a different one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that this is very likely, as the default one was not UTF-8 on PHP 5 (PHP 7 changed the default encoding to UTF-8)
@javiereguiluz, it's not wrong but those symbols are used rarely. Try to use: |
@@ -44,7 +44,7 @@ public function getSlugs() | |||
yield ['!Lorem Ipsum!', '!lorem-ipsum!']; | |||
yield ['lorem-ipsum', 'lorem-ipsum']; | |||
yield ['lorem 日本語 ipsum', 'lorem-日本語-ipsum']; | |||
yield ['lorem ру́сский язы́к ipsum', 'lorem-ру́сский-язы́к-ipsum']; | |||
yield ['lorem русский языќк ipsum', 'lorem-русский язык-ipsum']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiereguiluz, you forgot to delete odd ќ
And it's working 🎉 Thank you all for your help! |
In the Symfony Installer we recently made a change to forbid any non-Latin character in the project names (see details). This is needed because Composer itself requires that for package names.
In the Symfony Demo app we had a similar problem with the slugs of the blog posts. We removed any non-Latin character ... but that caused bugs like #136. I propose to change the slugger completely and accept any character.
For example, you can now create this blog post:
And the blog listing no longer displays an error:
And the URL is perfectly fine although it contains non-Latin chars: