The word “slug” was probably popularized by wordpress thanks to its ability to build pretty looking urls or permalinks for all of your post title.
Cakephp has a class known as Inflector and a slug method which does the same thing to help you get a pretty looking slug for any text strings you pass into it. Here’s how.
Let’s say you have a string variable:
$str = “SEO Keyword Targeting with Permalinks”
In any of your controller’s action, just do this
$permalink = Inflector::slug($str, ‘-‘);
and you’ll have “SEO-Keyword-Targeting-with-Permalinks” for your permalink.
Need all letter to be lowercase? No problem, just use the php function strtolower
$permalink = Inflector::slug(strtolower($str), ‘-‘);
Note that Inflector::slug expects a utf8 encoded string, so in order to take care of strings such as “apple purée”, you should doing
$permalink = Inflector::slug(utf8_encode(strtolower(apple purée)), ‘-‘);
Brunno Vodola Martins says
Exactly what I was looking for. Thanks! =)