用于印地语和阿拉伯语字符串的Str :: slug替代品?

Ari*_*ona 5 unicode multilingual laravel laravel-4

Str::slug用来生成友好的URL,但是Str::slug()方法返回null阿拉伯语和印地语字符串.可能也是中国人,日本人,韩国人和那些字符集.

例如:

return Str::slug('???????'); //null
Run Code Online (Sandbox Code Playgroud)

如何有效地解决这个问题?

Amr*_*Amr 6

当我使用阿拉伯语时,我遇到了这个问题,所以我做了以下功能,为我解决了这个问题.

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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,此函数仅解决阿拉伯语的问题,如果要解决印地语或任何其他语言的问题,则需要在这些????????????????????????????????????现有阿拉伯字符旁边或替代这些现有的阿拉伯字符添加印地语字符(或其他语言的字符).


小智 2

尝试这个:

节省:

Str::slug(Input::get('title'))==""?strtolower(urlencode(Input::get('title'))):Str::slug(Input::get('title'));
Run Code Online (Sandbox Code Playgroud)

得到:

 $slug = strtolower(urlencode($slug));
Run Code Online (Sandbox Code Playgroud)