Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Str::slug alternative for hindi and arabic strings?

I use Str::slug to generate friendly URL's, however Str::slug() method returns null on arabic and hindi strings. Probably chinese, japanese, korean and those charsets too.

For example:

return Str::slug('मनोरंजन'); //null

How can I solve this issue efficiently?

like image 375
Aristona Avatar asked Mar 01 '14 08:03

Aristona


2 Answers

I have faced this problem when I was working with Arabic language, so I've made the following function which solved the problem for me.

function make_slug($string = null, $separator = "-") {
    if (is_null($string)) {
        return "";
    }

    // Remove spaces from the beginning and from the end of the string
    $string = trim($string);

    // Lower case everything 
    // using mb_strtolower() function is important for non-Latin UTF-8 string | more info: http://goo.gl/QL2tzK
    $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 arabic charactrs as well
    $string = preg_replace("/[^a-z0-9_\s-ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]/u", "", $string);

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

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

    return $string;
}

Note that this function solves the problem only for Arabic language, if you want to solve the problem for Hindi or any other language, you need to add Hindi characters (or the other language's characters) beside or instead of these ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى existing Arabic characters.

like image 163
Amr Avatar answered Oct 31 '22 22:10

Amr


try this:

Save:

Str::slug(Input::get('title'))==""?strtolower(urlencode(Input::get('title'))):Str::slug(Input::get('title'));

Get:

 $slug = strtolower(urlencode($slug));
like image 25
idthappy Avatar answered Oct 31 '22 22:10

idthappy