I am in search of the best way to "slugify" string what "slug" is, and my current solution is based on this recipe
I have changed it a little bit to:
s = 'String to slugify'
slug = unicodedata.normalize('NFKD', s)
slug = slug.encode('ascii', 'ignore').lower()
slug = re.sub(r'[^a-z0-9]+', '-', slug).strip('-')
slug = re.sub(r'[-]+', '-', slug)
Anyone see any problems with this code? It is working fine, but maybe I am missing something or you know a better way?
You might consider changing the last line to
since the pattern
[-]+
is no different than-+
, and you don't really care about matching just one hyphen, only two or more.But, of course, this is quite minor.
This is the slugify function present in django.utils.text This should suffice your requirement.
There is python package named awesome-slugify:
Works like this:
awesome-slugify github page
Unidecode is good; however, be careful: unidecode is GPL. If this license doesn't fit then use this one