Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Created May 5, 2011 09:00
Show Gist options
  • Save jakejscott/956763 to your computer and use it in GitHub Desktop.
Save jakejscott/956763 to your computer and use it in GitHub Desktop.
Slug-a-fy-r
public static class Slugifyer {
public static string Slugify(this string phrase)
{
string str = phrase.RemoveAccent().ToLower();
str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars
str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
str = Regex.Replace(str, @"\s", "-"); // hyphens
return str;
}
public static string RemoveAccent(this string txt)
{
byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
return System.Text.Encoding.ASCII.GetString(bytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment