且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Yii - 使字符串在 URL 或文件名中可用

更新时间:2022-05-07 22:31:10

slugify in Django 转换为小写,删除非单词字符(字母数字和下划线)并将空格转换为连字符.还去除前导和尾随空格.
以下是 PHP 中执行相同任务的函数.

slugify in Django Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace.
Following are the functions in PHP to carry out same tasks.

$slug = preg_replace('@[\s!:;_\?=\\\+\*/%&#]+@', '-', $str);
      //this will replace all non alphanumeric char with '-'
$slug = mb_strtolower($slug);
      //convert string to lowercase
$slug = trim($slug, '-');
      //trim whitespaces

您需要在某些控制器中定义一个函数才能在 Yii 中使用它

You need to define a function in some controller TO use it in Yii