为了账号安全,请及时绑定邮箱和手机立即绑定

用于制作slug的PHP函数(URL字符串)

用于制作slug的PHP函数(URL字符串)

PHP
哔哔one 2019-08-06 13:45:11
用于制作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教程中的一个。


查看完整回答
反对 回复 2019-08-06
  • 3 回答
  • 0 关注
  • 610 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号