Framework: Str::slug no support for Arabic, Farsi, .. languages

Created on 20 May 2014  ·  10Comments  ·  Source: laravel/framework

There is no support for Arabic, Farsi, .. languages in Str::slug , I hope it will be in the next updates.
Is there any package for this ?
Thanks

Most helpful comment

@safir187 Yes actually this is the correct version:

function make_slug($string, $separator = '-')
{
    $string = trim($string);
    $string = mb_strtolower($string, 'UTF-8');
    $string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string);
    $string = preg_replace("/[\s-_]+/", ' ', $string);
    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
}

All 10 comments

Laravel use nicolas-grekas/Patchwork-UTF8 to convert utf-8 character into ascii code. I think you should open an issue in that project instead.

I speak Persian and I created this in my helpers file. You can do the same.

function make_slug($string, $separator = '-')
{
    $string = trim($string);
    $string = mb_strtolower($string, 'UTF-8');

    // Make alphanumeric (removes all other characters)
    // this makes the string safe especially when used as a part of a URL
    // this keeps latin characters and Persian characters as well
    $string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكگلمنوی]/u", '', $string);

    // Remove multiple dashes or whitespaces or underscores
    $string = preg_replace("/[\s-_]+/", ' ', $string);

    // Convert whitespaces and underscore to the given separator
    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
}

@hassanazimi looks like you don't have هـ there, right?

@safir187 Yes actually this is the correct version:

function make_slug($string, $separator = '-')
{
    $string = trim($string);
    $string = mb_strtolower($string, 'UTF-8');
    $string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string);
    $string = preg_replace("/[\s-_]+/", ' ', $string);
    $string = preg_replace("/[\s_]/", $separator, $string);

    return $string;
}

how about this

function make_slug($string, $separator = '-')
{
$string = trim($string);
$string = mb_strtolower($string, 'UTF-8');

/*
    ء-ي standard letters
    پچژکگیۃۂۀہ other common letters
    ۰-۹ persian digits
    ٠-٩ arabic digits
    ٱ-ۓ non standard arabic
    ،-۹ all and everything
*/

$string = preg_replace("/[^a-z0-9_\s-ء-يپچژکگیۃۂۀہ]/u", '', $string);
$string = preg_replace("/[\s-_]+/", ' ', $string);
$string = preg_replace("/[\s_]/", $separator, $string);

return $string;

@safir187 that's good. I used پ and چ and ژ and گ already (they don't exist in Arabic) but I don't use Urdo characters. Feel free to replace or add more characters.

Dear Hassan,

You can use this repository to generate slug for Persian words. I have trained that model. Please let me know if you need more information.

https://github.com/tihu-nlp/g2p-seq2seq-tihudict

@hassanazimi where should i add this code in laravel??

@OmarGarhy In your global helpers. (already mentioned)

I speak Persian and I created this in my helpers file. You can do the same.

@safir187 Yes actually this is the correct version:

function make_slug($string, $separator = '-')
{
  $string = trim($string);
  $string = mb_strtolower($string, 'UTF-8');
  $string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string);
  $string = preg_replace("/[\s-_]+/", ' ', $string);
  $string = preg_replace("/[\s_]/", $separator, $string);

  return $string;
}

this function not work in php 7.3 because PCRE engine migrates to PCRE2 and i updated your function for php =>7.3:

function make_slug($string, $separator = '-')
    {
        $string = trim($string);
        $string = mb_strtolower($string, 'UTF-8');
        $string = preg_replace("/[^a-z0-9_\-\sءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string);
        $string = preg_replace("/[\s\-_]+/", ' ', $string);
        $string = preg_replace("/[\s_]/", $separator, $string);

        return $string;
    }
Was this page helpful?
0 / 5 - 0 ratings