Str::slug alternative for hindi and arabic strings?

浪子不回头ぞ 提交于 2019-12-05 01:41:56

问题


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?


回答1:


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.




回答2:


try this:

Save:

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

Get:

 $slug = strtolower(urlencode($slug));


来源:https://stackoverflow.com/questions/22112029/strslug-alternative-for-hindi-and-arabic-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!