Replace all characters that aren’t letters and numbers with a hyphen [duplicate]

This should do what you’re looking for: function clean($string) { $string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens. return preg_replace(‘/[^A-Za-z0-9\-]/’, ”, $string); // Removes special chars. } Usage: echo clean(‘a|”bc!@£de^&$f g’); Will output: abcdef-g Edit: Hey, just a quick question, how can I prevent multiple hyphens from being next to each … Read more

django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.slug

The reason for this constrain could be that you didn’t have any field called slug in Category class when you have initially migrated it (First Migration), and after adding this field in the model, when you ran makemigrations, you have set default value to something static value(i.e None or ” etc), and which broke the … Read more

String slugification in Python

There is a python package named python-slugify, which does a pretty good job of slugifying: pip install python-slugify Works like this: from slugify import slugify txt = “This is a test —” r = slugify(txt) self.assertEquals(r, “this-is-a-test”) txt = “This — is a ## test —” r = slugify(txt) self.assertEquals(r, “this-is-a-test”) txt=”C\”est déjà l\’été.’ r … Read more

How to make Django slugify work properly with Unicode strings?

There is a python package called unidecode that I’ve adopted for the askbot Q&A forum, it works well for the latin-based alphabets and even looks reasonable for greek: >>> import unidecode >>> from unidecode import unidecode >>> unidecode(u’διακριτικός’) ‘diakritikos’ It does something weird with asian languages: >>> unidecode(u’影師嗎’) ‘Ying Shi Ma ‘ >>> Does this … Read more

How can I create a friendly URL in ASP.NET MVC?

There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter: routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}/{ignoreThisBit}”, new { controller = “Home”, action = “Index”, id = “”, ignoreThisBit = “”} // Parameter defaults ) Now you can type whatever you want to … Read more

Generate SEO friendly URLs (slugs) [closed]

I like the php-slugs code at google code solution. But if you want a simpler one that works with UTF-8: function format_uri( $string, $separator=”-” ) { $accents_regex = ‘~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i’; $special_cases = array( ‘&’ => ‘and’, “‘” => ”); $string = mb_strtolower( trim( $string ), ‘UTF-8’ ); $string = str_replace( array_keys($special_cases), array_values( $special_cases), $string ); $string … Read more

URL Slugify algorithm in C#?

http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html public static string GenerateSlug(this string phrase) { string str = phrase.RemoveAccent().ToLower(); // invalid chars str = Regex.Replace(str, @”[^a-z0-9\s-]”, “”); // convert multiple spaces into one space str = Regex.Replace(str, @”\s+”, ” “).Trim(); // cut and trim str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); str = Regex.Replace(str, @”\s”, “-“); // hyphens return … Read more

What is the etymology of ‘slug’ in a URL? [closed]

The term ‘slug’ comes from the world of newspaper production. It’s an informal name given to a story during the production process. As the story winds its path from the beat reporter (assuming these even exist any more?) through to editor through to the “printing presses”, this is the name it is referenced by, e.g., … Read more

How do I create a slug in Django?

You will need to use the slugify function. >>> from django.template.defaultfilters import slugify >>> slugify(“b b b b”) u’b-b-b-b’ >>> You can call slugify automatically by overriding the save method: class Test(models.Model): q = models.CharField(max_length=30) s = models.SlugField() def save(self, *args, **kwargs): self.s = slugify(self.q) super(Test, self).save(*args, **kwargs) Be aware that the above will cause … Read more