Yii - 使字符串在 URL 或文件名中可用
Yii 框架是否包含可以使字符串在 URL 或文件名中可用的函数?
例如:Health+%26+Safety+franchises
= health-safety-franchises
所以类似于:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify
Does the Yii framework contain a function that can make a string usable in a URL or filename ?
For example: Health+%26+Safety+franchises
= health-safety-franchises
So something similar to: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Django 中的 slugify 转换为小写,删除非单词字符(字母数字和下划线)并将空格转换为连字符。还去除前导和尾随空白。
以下是 PHP 中执行相同任务的函数。
你需要在某个控制器中定义一个函数才能在 Yii 中使用它
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.
You need to define a function in some controller TO use it in Yii
目前尚不完全清楚您到底想要实现什么目标。
如果您想使用包含浏览器不支持的字符的字符串,那么您应该研究可以为您执行此操作的 php 函数。
也许
http://php.net/manual/en/function.urlencode.php
(还有更多,取决于您的需要)
如果您想使用自己的自定义编码,请指定您想要实现的目标,我可能会提供帮助。
It is still not completely clear what you are trying to achieve exactly.
If you want to use a string that contains characters that are not supported by the browser then you should look into php functions that can do this for you.
Perhaps
http://php.net/manual/en/function.urlencode.php
(there are more, depends what you need)
If you want to use your own custom encoding then specify what your trying to achieve and I might be able to help.
您可以向模型添加一个行为 - 这个插件将为您完成这项艰苦的工作。
You can add a behaviour to a model for that - this plugin will do the hard work for you.
看看这个类到 yii2
https://github. com/yiisoft/yii2/blob/master/framework/behaviors/SluggableBehavior.php
并查看它如何使用这个库http://www.yiiframework.com/doc-2.0/yii-helpers- inflector.html slug 方法
Have a look to this class into yii2
https://github.com/yiisoft/yii2/blob/master/framework/behaviors/SluggableBehavior.php
and see how it use this library http://www.yiiframework.com/doc-2.0/yii-helpers-inflector.html the slug method