Make Laravel slug support utf8 characters

If you ever wanted to use Laravel Str:slug() method, you maybe noticed it only supports latin characters. And you may open Str class (\vendor\laravel\framework\src\Illuminate\Support\Str.php) and see:

public static function slug($title, $separator = '-')
{
  $title = static::ascii($title);

  // Convert all dashes/underscores into separator
  $flip = $separator == '-' ? '_' : '-';
  $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);

  // Remove all characters that are not the separator, letters, numbers, or whitespace.
  $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));

  // Replace all separator characters and whitespace by a single separator
  $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

  return trim($title, $separator);
}

You see at the very first line that string convert to ansii. That's why you cannot use utf8 characters on return. You can easily comment that line out and see the result. But editing the Laravel source it's not goof approach.

For changing slug method functionality it's better to extend or override this method. For doing this, first create a file name app/macros.php , then copy/paste main slug method into this file and convert it to a closure method:

Str::macro('slug_utf8', function($title, $separator = '-')
{
    //$title = static::ascii($title); //comment it out to suport farsi

    // Convert all dashes/underscores into separator
    $flip = $separator == '-' ? '_' : '-';

    $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);

    // Remove all characters that are not the separator, letters, numbers, or whitespace.
    $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));

    // Replace all separator characters and whitespace by a single separator
    $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

    return trim($title, $separator);

});

Before this method be usable in your application, you should add this macro.php file at the app/start/global.php, open this file and at the end of file add this line of code:

require app_path().'/macros.php';

Now you have a slug_utf8 method that can be used in your applications:

$slug = Str::slug_utf8($value);

Comments

Posted by Lazrypoono on
cialis from canada over the counter last post
http://cialiswithoutadoctorsprescription.org - cialis without a doctor's prescription
cialis 10 mg effectiveness jump to
cialis without a doctor's prescription
- cialis commercial black couple last post by
cialis 20mg msn messenger
Posted by JazonFeedo on
no prescription viagra everyday
http://viagra-withoutdoctorprescriptions.org - viagra without doctor prescription
like viagra for heart help that insurance pays
viagra without a doctor prescription
- your name subject comment inurl:comment/reply/2 viagra
viagra and alcohol side effects total members