Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create slugify in symfony2?

Tags:

php

slug

symfony

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.

like image 217
gastonnina Avatar asked Jun 23 '13 02:06

gastonnina


2 Answers

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

like image 93
Abdoul Ndiaye Avatar answered Sep 23 '22 16:09

Abdoul Ndiaye


Use either l3ppard's sluggable extension ( GitHub repository) ...

... or KnpLabs sluggable behavior ( GitHub repository ).

like image 29
Nicolai Fröhlich Avatar answered Sep 24 '22 16:09

Nicolai Fröhlich