« My Foxification (Pimp Your Firefox to be a web all-star) | Home | Their Toolbar »
My inc.functions.php – Part 2 : Creating a post slug
In 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 (http://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...
-
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 ...
About this entry

Dec 18 2006 / 11:25 pm


Best of Bram.us
Start getting used to scratching and smudging the iPhone with this Flash based iPhone Simulator
Automatically wrap/float/contour text around freeform images using CSS for usage in (X)HTML pages.
Pimp Your Firefox to be a web all-star
A look at the inc.common.php, spreading handy functions.
Photoshop Script inspired in the Chlomoscript to unobtrusively pimp photos you have taken.
Methods to adding file management options to TinyMCE (Part 1), Extending TinyFCK (Part 2) and PHP file uploads overview (Spinoff 1).




Trackbacks/Pings
[...] function is taken from Bramus and his excellent article about creating a post slug, and it does the job very [...]
Comments
This function is what we call dirify()
http://kalsey.com/2004/07/dirify_in_php/
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)));
}