Yii - 使字符串在 URL 或文件名中可用 [英] Yii - Make a string usable in a URL or filename
本文介绍了Yii - 使字符串在 URL 或文件名中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Yii 框架是否包含可以使字符串在 URL 或文件名中可用的函数?
Does the Yii framework contain a function that can make a string usable in a URL or filename ?
例如:Health+%26+Safety+franchises
= health-safety-franchises
所以类似于:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify
推荐答案
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
这篇关于Yii - 使字符串在 URL 或文件名中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文