I have started to use symfony2 and I have found some functions that exists in symfony 1.4 like slugify. I need this in order to improve the furl.
Asked
Active
Viewed 1.7k times
3 Answers
10
If you have php-intl :
/**
* Transform (e.g. "Hello World") into a slug (e.g. "hello-world").
*
* @param string $string
*
* @return string
*/
public function slugify($string)
{
$rule = 'NFD; [:Nonspacing Mark:] Remove; NFC';
$transliterator = \Transliterator::create($rule);
$string = $transliterator->transliterate($string);
return preg_replace(
'/[^a-z0-9]/',
'-',
strtolower(trim(strip_tags($string)))
);
}
Otherwise, have a look on the doctrine extensions

Abdoul Ndiaye
- 193
- 1
- 7
-
This answer is very simple and seems to work great so far as long as Transliterator is enabled. Thank you. – mr1031011 Oct 03 '15 at 15:19
-
3not enough people know that transforming a non-ascii input (like french with accents or æ that should be transformed into ae etc.) into ascii is a problem long solved in a standard way by unicode – allan.simon Nov 16 '16 at 00:05
-
Doctrine extension is using [Behat Transliterator](https://github.com/Behat/Transliterator) – ryabenko-pro May 03 '17 at 04:18
5
Use either l3ppard's sluggable extension ( GitHub repository) ...
... or KnpLabs sluggable behavior ( GitHub repository ).

Nicolai Fröhlich
- 51,330
- 11
- 126
- 130
-
thanks a lot nifr. I'll try them. I have also see [stof](https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst). Witch do you think is the best option – gastonnina Jun 23 '13 at 15:02
-
StofDoctrineExtensions provides easier integration ( registering services, setting up the listeners ) in symfony2 but not the extensions themselves. – Nicolai Fröhlich Jun 23 '13 at 16:52
-
Late to the game but a non unique slug is next to useless for most use cases. – RichieHH Sep 29 '17 at 11:02
-
I've found this library - https://github.com/cocur/slugify. Works great. – Sergiy Zaharchenko Nov 22 '19 at 20:35
2
I used util.php for slugify. It doesn't require Symfony though, but works well with it.
util::slugify('This is a random --string with an Ãccent');
=> Returns 'this-is-a-random-string-with-an-accent'

gorodezkiy
- 3,299
- 2
- 34
- 42