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?
Great !
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, '-'));
});
Don’t use a client-side slug creator, as it can be maliciously exploited. If an attacker wants to insert special characters into a slug for whatever reason, they can easily bypass the client-side script and send the malicious slug to the server. If the server doesn’t perform any additional checking/modification on the submitted slug (which if it did then a client-side slug creator isn’t necessary), then the attacker can successfully exploit the server.
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:
Original code already does this. Caret ^ means NOT in RegEx. Per the example, author only allows letters, numbers, and hyphens ;)
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/
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:
cool tips, but I can restrics duplicate slug
BUMP
You would need to connect to a database to do that.
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!
Agree
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!
100% working
my title is बोन कैंसर: जानें, क्यों नहीं नजरअंदाज करना चाहिए इन संकेतों को?
i want to change into बोन-कैंसर-जानें-क्यों-नही.html
not working with above code any suggestion.
Thanks.
danke Davinder Singh :-)
und das ganze für deutsche umlaute erweitert einfach genial
function getSlug($text)
{
$word = strtolower($text);
$word = str_replace(“ä”,”ae”,$word);
$word = str_replace(“ö”,”oe”,$word);
$word = str_replace(“ü”,”ue”,$word);
$word = str_replace(“ß”,”ss”,$word);
$word = str_replace(“&”,”und”,$word);
$text = preg_replace(‘~[^\pL\d]+~u’, ‘-‘, $word);
$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;
}
Thanx its really worth and work for me…:p
Another way to do that simply :
creating slug in this way is limited to English alphabets. it’s better to first use function explode and space as delimiter to convert to an array and then implode it with optional delimiter.
}
This nice script will handle every aspect. Including removing special characters, and handling different languages
https://github.com/cocur/slugify
In WP, I use a nice post title formatter for this purpose
I created a library to generate slugs: https://github.com/ausi/slug-generator
great work
This will be more efficient.
function slugify(string) {
const a = ‘àáäâãåèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;’
const b = ‘aaaaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh——‘
const p = new RegExp(a.split(”).join(‘|’), ‘g’)
If you are working with PHP- Codeigniter framework and you want to Generate URL slug in Codeigniter then follow this link
https://datainflow.com/generate-url-slug-codeigniter/