我在使用阿拉伯语时遇到了这个问题,所以我制作了以下功能,为我解决了这个问题。
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;
}
此功能仅解决阿拉伯语的问题,如果要解决西里尔文或任何其他语言的问题,您需要在这些现有的阿拉伯字符旁边或代替这些ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى
现有的阿拉伯字符添加西里尔字符(或其他语言的字符)。