gpt4 book ai didi

unicode - Str::slug 替代印地语和阿拉伯语字符串?

转载 作者:行者123 更新时间:2023-12-02 16:24:26 25 4
gpt4 key购买 nike

我使用 Str::slug 生成友好的 URL,但是 Str::slug() 方法在阿拉伯语和印地语字符串上返回 null 。可能还有中文、日文、韩文和那些字符集。

例如:

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

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

最佳答案

我在使用阿拉伯语言时遇到了这个问题,所以我制作了以下函数来解决这个问题。

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

请注意,此函数仅解决阿拉伯语的问题,如果您想解决印地语或任何其他语言的问题,您需要在这些 ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى 旁边添加印地语字符(或其他语言的字符)或代替这些字符现有的阿拉伯字符。

关于unicode - Str::slug 替代印地语和阿拉伯语字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com