Yii - Make a string usable in a URL or filename

2019-07-21 06:55发布

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

标签: php url yii slug
4条回答
等我变得足够好
3楼-- · 2019-07-21 07:34

You can add a behaviour to a model for that - this plugin will do the hard work for you.

查看更多
再贱就再见
4楼-- · 2019-07-21 07:39

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.

查看更多
Fickle 薄情
5楼-- · 2019-07-21 07:42

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

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

查看更多
登录 后发表回答