【发布时间】:2020-04-17 07:38:44
【问题描述】:
我正在使用 Laravel,我使用 cviebrock/eloquent-sluggable 为产品制作 slug
所以当我使用英文标题时效果很好,但每当我使用波斯语时,例如“تست” slug 保存为像这样“tst”这样的英文翻译 我改变了sluggable的方法
'method' => function ($string, $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:
$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;
},
但结果还是一样
【问题讨论】:
-
我认为
u应该出现在正则表达式中的结尾/之后。另外#放在那里可能是错误的