Yii2: Inflector::slug() replacement replaced by actual replacement

Created on 22 Nov 2017  路  4Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

$column = 'this is a test';
$column = Inflector::slug($column, '_'); //result: this_is_a_test
$column = Inflector::slug($column, '_'); //result: thisisatest

What is the expected result?

this_is_a_test
It should not replace the replacements

What do you get instead?

thisisatest

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.13.1
| PHP version | 7.1.4
| Operating system | Windows 10

ready for adoption bug

Most helpful comment

I agree this to be strange behavior.
It makes no sense to replace and remove the same character at the same time.

All 4 comments

I'd say this is expected behaviour as per the docs, http://www.yiiframework.com/doc-2.0/yii-helpers-baseinflector.html#slug()-detail

Returns a string with all spaces converted to given replacement, non word characters removed and the rest of characters transliterated.

It only replaces spaces so any subsequent calls would remove the replacements.

I didn't read the docs, i just assumed slug() would do the trick and make me a slug() ;-). Maybe it should be this way? Or renaming the function to replaceSpaces()?

I agree this to be strange behavior.
It makes no sense to replace and remove the same character at the same time.

The default works for a -, you'd need to include the replacement string in the regex.
Could use http://php.net/manual/en/function.preg-quote.php to add it.

Was this page helpful?
0 / 5 - 0 ratings