Alex
Alex

Reputation: 4774

Slugs with non-english letters

I have been using Laravel's Str::slug function and I realized that it doesn't create a slug at all if the user only submits non-english letters.

I have been Googling this for a while and I'm unable to find a solution.

Did any of you encounter this and found a fix?

Upvotes: 2

Views: 849

Answers (1)

Denis Mysenko
Denis Mysenko

Reputation: 6534

Since some browsers and applications still don't display unicode URLs nicely, I would suggest to transliterate your international slugs instead - make them look latin. I personally use this for one of my projects:

public static function slugify($text) {
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    $text = trim($text, '-');
    if (function_exists('transliterator_transliterate')) $text = transliterator_transliterate('Any-Latin; Latin-ASCII', $text);
    $text = iconv('utf-8', 'ASCII//TRANSLIT//IGNORE', $text);
    $text = strtolower($text);
    $text = preg_replace('~[^-\w]+~', '', $text);

    return $text;
}

Upvotes: 3

Related Questions