Slugify strings

Published on and tagged with cakephp  feature

In a recent changeset a nice, little function has been introduced: Inflector::slug(). Such a function is used for example in blogs to create urls from titles.

Its usage is simple:

echo Inflector::slug('This is a test');
// outputs: This_is_a_test

echo Inflector::slug('This is a test', '-');
// outputs: This-is-a-test

You have to be careful with this function if you use it for example with german umlauts, as you may get an unexpected result:

echo Inflector::slug('Es sind Kühe auf der Wiese');
// outputs: Es_sind_K_he_auf_der_Wiese

© daniel hofstetter. Licensed under a Creative Commons License