Skip to content

Commit 8f40b6f

Browse files
committed
bug #389 Made the slugger compatible with every language (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #389). Discussion ---------- Made the slugger compatible with every language In the Symfony Installer we recently made a change to forbid any non-Latin character in the project names ([see details](symfony/symfony-installer#278)). 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: ![backend_post](https://cloud.githubusercontent.com/assets/73419/19352609/b582a714-9160-11e6-81de-8e5130f1ec78.png) And the blog listing no longer displays an error: ![blog_post](https://cloud.githubusercontent.com/assets/73419/19352619/bfbd87d0-9160-11e6-89fa-ca60aee1b5fe.png) And the URL is perfectly fine although it contains non-Latin chars: ![url-post](https://cloud.githubusercontent.com/assets/73419/19352634/c9fc89c6-9160-11e6-999c-346b4da78653.png) Commits ------- 161b508 Made the slugger compatible with every language
2 parents f3d2d9a + 161b508 commit 8f40b6f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/AppBundle/Utils/Slugger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ class Slugger
2727
*/
2828
public function slugify($string)
2929
{
30-
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-');
30+
return preg_replace('/\s+/', '-', mb_strtolower(trim(strip_tags($string)), 'UTF-8'));
3131
}
3232
}

tests/AppBundle/Utils/SluggerTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ public function testSlugify($string, $slug)
3838

3939
public function getSlugs()
4040
{
41-
yield ['Lorem Ipsum' , 'lorem-ipsum'];
42-
yield [' Lorem Ipsum ' , 'lorem-ipsum'];
43-
yield [' lOrEm iPsUm ' , 'lorem-ipsum'];
44-
yield ['!Lorem Ipsum!' , 'lorem-ipsum'];
45-
yield ['lorem-ipsum' , 'lorem-ipsum'];
41+
yield ['Lorem Ipsum', 'lorem-ipsum'];
42+
yield [' Lorem Ipsum ', 'lorem-ipsum'];
43+
yield [' lOrEm iPsUm ', 'lorem-ipsum'];
44+
yield ['!Lorem Ipsum!', '!lorem-ipsum!'];
45+
yield ['lorem-ipsum', 'lorem-ipsum'];
46+
yield ['lorem 日本語 ipsum', 'lorem-日本語-ipsum'];
47+
yield ['lorem русский язык ipsum', 'lorem-русский-язык-ipsum'];
48+
yield ['lorem العَرَبِيَّة‎‎ ipsum', 'lorem-العَرَبِيَّة‎‎-ipsum'];
4649
}
4750
}

0 commit comments

Comments
 (0)