Marian
Marian

Reputation: 1164

Yii - Make a string usable in a URL or filename

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

Upvotes: 3

Views: 6082

Answers (4)

Federico Benedetti
Federico Benedetti

Reputation: 67

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

Upvotes: -1

Uday Sawant
Uday Sawant

Reputation: 5798

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

Upvotes: 4

Dan Blows
Dan Blows

Reputation: 21184

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

Upvotes: 0

Zerg Ling
Zerg Ling

Reputation: 233

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.

Upvotes: 1

Related Questions