My inc.functions.php – Part 2 : Creating a post slug

PHP Willow TreeIn the second part of this series (Click for part 1) another tiny, yet handy PHP function is described. Some of you might already have discovered the power of having a "post slug" in an URI. Not only does a post slug give an indication to the user (he can tell by the URI what the post/link is all about), it also improves your ranking within search engines as they mark a word in the URI as being important and relevant.

Basically a post slug only contains the alphanumerical characters and spaces get replaced by dashes. Above that it’s lowercase.

These 3 criteria can easily be achieved by using a regular expression to filter out the alphanumerical characters, followed by a simple str_replace to replace all spaces by dashes and finally followed by a lowercase() call.

// Bramus! pwnge! : simple method to create a post slug — https://www.bram.us/
function fixForUri($string) {
	$slug = preg_replace("/[^a-zA-Z0-9 -]/", "", $string); // only take alphanumerical characters, but keep the spaces and dashes too...
	$slug = str_replace(" ", "-", $slug); // replace spaces by dashes
	$slug = strtolower($slug);	// make it lowercase
	return $slug;
}

The function itself can of course be written into a single line, yet to make it all more readable it has been spread out. One call per line.

The reason I’ve named the function fixForUri() is due to the fact that the function actually fixes any given string for usage in an URI

Happy coding!

B!

Oh yeah, the next function coming up in this series will be a pure aesthetic one …

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation

3 Comments

  1. function remove_accent($str)
    {
    $a = array(‘À’,’Á’,’Â’,’Ã’,’Ä’,’Å’,’Æ’,’Ç’,’È’,’É’,’Ê’,’Ë’,’Ì’,’Í’,’Î’,’Ï’,’Ð’,’Ñ’,’Ò’,’Ó’,’Ô’,’Õ’,’Ö’,’Ø’,’Ù’,’Ú’,’Û’,’Ü’,’Ý’,’ß’,’à’,’á’,’â’,’ã’,’ä’,’å’,’æ’,’ç’,’è’,’é’,’ê’,’ë’,’ì’,’í’,’î’,’ï’,’ñ’,’ò’,’ó’,’ô’,’õ’,’ö’,’ø’,’ù’,’ú’,’û’,’ü’,’ý’,’ÿ’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’Œ’,’œ’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’Š’,’š’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’Ÿ’,’?’,’?’,’?’,’?’,’Ž’,’ž’,’?’,’ƒ’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’,’?’);
    $b = array(‘A’,’A’,’A’,’A’,’A’,’A’,’AE’,’C’,’E’,’E’,’E’,’E’,’I’,’I’,’I’,’I’,’D’,’N’,’O’,’O’,’O’,’O’,’O’,’O’,’U’,’U’,’U’,’U’,’Y’,’s’,’a’,’a’,’a’,’a’,’a’,’a’,’ae’,’c’,’e’,’e’,’e’,’e’,’i’,’i’,’i’,’i’,’n’,’o’,’o’,’o’,’o’,’o’,’o’,’u’,’u’,’u’,’u’,’y’,’y’,’A’,’a’,’A’,’a’,’A’,’a’,’C’,’c’,’C’,’c’,’C’,’c’,’C’,’c’,’D’,’d’,’D’,’d’,’E’,’e’,’E’,’e’,’E’,’e’,’E’,’e’,’E’,’e’,’G’,’g’,’G’,’g’,’G’,’g’,’G’,’g’,’H’,’h’,’H’,’h’,’I’,’i’,’I’,’i’,’I’,’i’,’I’,’i’,’I’,’i’,’IJ’,’ij’,’J’,’j’,’K’,’k’,’L’,’l’,’L’,’l’,’L’,’l’,’L’,’l’,’l’,’l’,’N’,’n’,’N’,’n’,’N’,’n’,’n’,’O’,’o’,’O’,’o’,’O’,’o’,’OE’,’oe’,’R’,’r’,’R’,’r’,’R’,’r’,’S’,’s’,’S’,’s’,’S’,’s’,’S’,’s’,’T’,’t’,’T’,’t’,’T’,’t’,’U’,’u’,’U’,’u’,’U’,’u’,’U’,’u’,’U’,’u’,’U’,’u’,’W’,’w’,’Y’,’y’,’Y’,’Z’,’z’,’Z’,’z’,’Z’,’z’,’s’,’f’,’O’,’o’,’U’,’u’,’A’,’a’,’I’,’i’,’O’,’o’,’U’,’u’,’U’,’u’,’U’,’u’,’U’,’u’,’U’,’u’,’A’,’a’,’AE’,’ae’,’O’,’o’);
    return str_replace($a, $b, $str);
    }

    function post_slug($str)
    {
    return strtolower(preg_replace(array(‘/[^a-zA-Z0-9 -]/’, ‘/[ -]+/’, ‘/^-|-$/’), array(”, ‘-‘, ”), remove_accent($str)));
    }

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.