Framework: str_slug convert my arabic letters into English due to ASCII method

Created on 5 Jun 2016  ·  9Comments  ·  Source: laravel/framework

I suggest that you add a param to allow user to choose allow ASCII conversion or not.

public static function slug($title, $separator = '-', $enable_ascii=true)
{
       if($enable_ascii) {
            $title = static::ascii($title);
       }
       ...

Most helpful comment

preg_match(): Compilation failed: invalid range in character class at offset 11

if you get this error and php version is 7.3 use \- instead of -

[^a-z0-9_\s\-
and

"/[\s\-_]+/"

All 9 comments

Feel free to add your own function to your app. We're intentionally forcing ascii in uris.

I speak 3 languages. For Farsi use this:

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;
}

Thank u very much mr Azimi. I use your code but i added some character for example other unicode of ک ه and farsi numbers.

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;

}

this is amazing

Using @hassanazimi 's solution, you may also want to skip punctuation using the following quick fix:

//...
$arabic_punct = ['،', '؛', '؟', '⠐', '!'];

foreach ($arabic_punct as $punct) {
    $string = preg_replace("#".mb_strtolower($punct, 'UTF-8')."#", '', $string);
}
//...

@GrahamCampbell since the main purpose of slugs is SEO this doesn't seem like a wise choice, does it?

Thanks @mhmalekian for your function it actually works great at arabic but I noticed that it strips the " إ , يـ ,ة" characters from the string ,, here is slight update to fix this :

//slugify arabic characters string function
public 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;
}

}

preg_match(): Compilation failed: invalid range in character class at offset 11

if you get this error and php version is 7.3 use \- instead of -

[^a-z0-9_\s\-
and

"/[\s\-_]+/"

Thank you all for your contribution.
and special Thanks, @mohammad6006 you made my day.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

fideloper picture fideloper  ·  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  ·  3Comments

PhiloNL picture PhiloNL  ·  3Comments

Fuzzyma picture Fuzzyma  ·  3Comments