Create URL Slug from Post Title
Regular expression function that replaces spaces between words with hyphens.
<?php
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>
Maybe you can add a strtolower there?
Here is jquery (client side) slug creator.
$('#title').change(function() {$(this).val($.trim($(this).val()));
// Trim empty space
$(this).val($(this).val().replace(/\s+/g,' '));
// replace more then 1 space with only one
$('#url').val($(this).val().toLowerCase());
$('#url').val($('#url').val().replace(/\W/g, ' '));
$('#url').val($.trim($('#url').val()));
$('#url').val($('#url').val().replace(/\s+/g, '-'));
});
Stop words are always a nice accompaniment to slug creators,
function remove_accent($str){
$a = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','Ā','ā','Ă','ă','Ą','ą','Ć','ć','Ĉ','ĉ','Ċ','ċ','Č','č','Ď','ď','Đ','đ','Ē','ē','Ĕ','ĕ','Ė','ė','Ę','ę','Ě','ě','Ĝ','ĝ','Ğ','ğ','Ġ','ġ','Ģ','ģ','Ĥ','ĥ','Ħ','ħ','Ĩ','ĩ','Ī','ī','Ĭ','ĭ','Į','į','İ','ı','IJ','ij','Ĵ','ĵ','Ķ','ķ','Ĺ','ĺ','Ļ','ļ','Ľ','ľ','Ŀ','ŀ','Ł','ł','Ń','ń','Ņ','ņ','Ň','ň','ʼn','Ō','ō','Ŏ','ŏ','Ő','ő','Œ','œ','Ŕ','ŕ','Ŗ','ŗ','Ř','ř','Ś','ś','Ŝ','ŝ','Ş','ş','Š','š','Ţ','ţ','Ť','ť','Ŧ','ŧ','Ũ','ũ','Ū','ū','Ŭ','ŭ','Ů','ů','Ű','ű','Ų','ų','Ŵ','ŵ','Ŷ','ŷ','Ÿ','Ź','ź','Ż','ż','Ž','ž','ſ','ƒ','Ơ','ơ','Ư','ư','Ǎ','ǎ','Ǐ','ǐ','Ǒ','ǒ','Ǔ','ǔ','Ǖ','ǖ','Ǘ','ǘ','Ǚ','ǚ','Ǜ','ǜ','Ǻ','ǻ','Ǽ','ǽ','Ǿ','ǿ');
$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)));
}
You would need also to remove punctuation symbols:
Its quite simple and good. Nice effort I must say.. I had also written one post for ASP Classic for same topic.
How to: Create SEO Friednly URL in ASP Classic
Thanks!
Agree
Excellent post ! very simple and good.
Thanks for putting it online.
@Weblap.ro Thanks for this special char. removal script. This makes this page very useful.
Sudeep
Infovinity
Another equivalent function that make better results : http://code.seebz.net/p/to-permalink/
This is the bees knees simple slug creation. Thank Chris!
how to make post slug when you language is utf-8 like “xin chào”. i don’t want to change like “xin cho” when i use your function.
My function like you:
Help me make change slug like string: “xin chào.”
Thanks
For Vietnamese language I used this array:
function remove_accent($str)
{
$a = array(
'á','é','í','ó','ú','ý','Á','É','Í','Ó','Ú','Ý',
'à','è','ì','ò','ù','ỳ','À','È','Ì','Ò','Ù','Ỳ',
'ả','ẻ','ỉ','ỏ','ủ','ỷ','Ả','Ẻ','Ỉ','Ỏ','Ủ','Ỷ',
'ã','ẽ','ĩ','õ','ũ','ỹ','Ã','Ẽ','Ĩ','Õ','Ũ','Ỹ',
'ạ','ẹ','ị','ọ','ụ','ỵ','Ạ','Ẹ','Ị','Ọ','Ụ','Ỵ',
'â','ê','ô','ư','Â','Ê','Ô','Ư',
'ấ','ế','ố','ứ','Ấ','Ế','Ố','Ứ',
'ầ','ề','ồ','ừ','Ầ','Ề','Ồ','Ừ',
'ẩ','ể','ổ','ử','Ẩ','Ể','Ổ','Ử',
'ậ','ệ','ộ','ự','Ậ','Ệ','Ộ','Ự'
);
$b = array(
'a','e','i','o','u','y','A','E','I','O','U','Y',
'a','e','i','o','u','y','A','E','I','O','U','Y',
'a','e','i','o','u','y','A','E','I','O','U','Y',
'a','e','i','o','u','y','A','E','I','O','U','Y',
'a','e','i','o','u','y','A','E','I','O','U','Y',
'a','e','o','u','A','E','O','U',
'a','e','o','u','A','E','O','U',
'a','e','o','u','A','E','O','U',
'a','e','o','u','A','E','O','U',
'a','e','o','u','A','E','O','U'
);
return str_replace($a, $b, $str);
}
cool tips, but I can restrics duplicate slug
For the quick and not-so-dirty way I’ve always used:
$slug = preg_replace(“/\W+/”,”-”,$text);
By using \W+ you take care of all non-latin characters.
No matter what I need done on a website I’m having trouble with I google what I want and your links never disappoint. Thanks Chris!
How can i add a button? So when i click on Auto genrerator frienly url
What about the built-in function for this? sanitize_title_with_dashes(); I use it all the time.
sanitize_title_with_dashes() is wordpress function :).
My bad! I thought you all were talking about WordPress :) Sorry!
function getSlug($text) { $text = preg_replace('~[^\\pL\d]+~u', '-', $text); $text = trim($text, '-'); $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); $text = strtolower($text); $text = preg_replace('~[^-\w]+~', '', $text); if (empty($text)) { return 'n-a'; } return $text; }100% working