Search code examples
cakephpcakephp-2.1slug

Can cakephp's inflector slug ignore specified characters?


What I'm trying to achieve is:

Let inflector slug Ignore a special character in a string, which would normally be replaced by the replacement string by inflector slug.

E.g. Character I want to be ignored: '/'

Input: this is an /example/

Output: this_is_an_example

Output I want: this_is_an_/example/

I have found the property '_uninflected' in the documention, but I don't think it's what I'm looking for (nor will it work for what I want).


Solution

  • You can do an explode and the slug each token.

    $input='/hi/im/a/non inflected ùrl/:=D';
    $tokens=explode('/',$input);
    foreach($tokens as &$token) $token=Inflector::slug($token);
    $output='this_is_an_'.implode('/',$tokens);