Skip to content

Instantly share code, notes, and snippets.

@leonardopinho
Created July 23, 2019 13:32
Show Gist options
  • Save leonardopinho/d5dbd174e9579d689161361b1e0036c5 to your computer and use it in GitHub Desktop.
Save leonardopinho/d5dbd174e9579d689161361b1e0036c5 to your computer and use it in GitHub Desktop.
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 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