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

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

URL Friendly Username in PHP?

function Slug($string) { return strtolower(trim(preg_replace(‘~[^0-9a-z]+~i’, ‘-‘, html_entity_decode(preg_replace(‘~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i’, ‘$1’, htmlentities($string, ENT_QUOTES, ‘UTF-8’)), ENT_QUOTES, ‘UTF-8’)), ‘-‘)); } $user=”Alix Axel”; echo Slug($user); // alix-axel $user=”Álix Ãxel”; echo Slug($user); // alix-axel $user=”Álix—-_Ãxel!?!?”; echo Slug($user); // alix-axel

Turn a string into a valid filename?

You can look at the Django framework for how they create a “slug” from arbitrary text. A slug is URL- and filename- friendly. The Django text utils define a function, slugify(), that’s probably the gold standard for this kind of thing. Essentially, their code is the following. import unicodedata import re def slugify(value, allow_unicode=False): “”” … 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

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