If you’re in the process of building a new custom CMS, you might want those clean shiny URL’s(or slugs). You can’t have spaces in it, neither can you have funny looking symbols. You also need to make sure its readable. After reading an article at Intrepid and at cubiq.org, I merged both those functions(that were provided) into one that would generate a perfect readable slug using PHP.
Here’s the PHP function to do so:
function create_slug($str) {
$result = @iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$result = strtolower($result);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/\s+/", " ", $result));
$result = preg_replace("/\s/", "-", $result);
$result = preg_replace("/[\/_|+ -]+/", '-', $result);
return $result;
}
This is the slug generated for this post title:
perfect-function-to-create-a-readable-slug-with-php
No comments
No comments yet...