Java code/library for generating slugs (for use in pretty URLs)

Normalize your string using canonical decomposition: private static final Pattern NONLATIN = Pattern.compile(“[^\\w-]”); private static final Pattern WHITESPACE = Pattern.compile(“[\\s]”); public static String toSlug(String input) { String nowhitespace = WHITESPACE.matcher(input).replaceAll(“-“); String normalized = Normalizer.normalize(nowhitespace, Form.NFD); String slug = NONLATIN.matcher(normalized).replaceAll(“”); return slug.toLowerCase(Locale.ENGLISH); } This is still a fairly naive process, though. It isn’t going to do … 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

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 convert a Title to a URL slug in jQuery?

I have no idea where the ‘slug’ term came from, but here we go: function convertToSlug(Text) { return Text.toLowerCase() .replace(/ /g, ‘-‘) .replace(/[^\w-]+/g, ”); } The first replace method will change spaces to hyphens, second, replace removes anything not alphanumeric, underscore, or hyphen. If you don’t want things “like – this” turning into “like—this” then … Read more

PHP function to make slug (URL string)

Instead of a lengthy replace, try this one: public static function slugify($text, string $divider=”-“) { // replace non letter or digits by divider $text = preg_replace(‘~[^\pL\d]+~u’, $divider, $text); // transliterate $text = iconv(‘utf-8’, ‘us-ascii//TRANSLIT’, $text); // remove unwanted characters $text = preg_replace(‘~[^-\w]+~’, ”, $text); // trim $text = trim($text, $divider); // remove duplicate divider $text … 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

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 does Stack Overflow generate its SEO-friendly URLs?

Here’s how we do it. Note that there are probably more edge conditions than you realize at first glance. This is the second version, unrolled for 5x more performance (and yes, I benchmarked it). I figured I’d optimize it because this function can be called hundreds of times per page. /// <summary> /// Produces optional, … Read more

Remove all special characters from a string [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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)