用于制作slug的PHP函数(URL字符串)我想要一个函数来创建来自Unicode字符串的slugs,例如gen_slug('Andrés Cortez')应该返回andres-cortez。我该怎么办?
3 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
尝试这个,而不是冗长的替换:
public static function slugify($text){
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;}这是基于Symfony的Jobeet教程中的一个。
- 3 回答
- 0 关注
- 629 浏览
添加回答
举报
0/150
提交
取消
