Skip to content

Instantly share code, notes, and snippets.

@BenRhouma
Created December 25, 2015 09:18
Show Gist options
  • Save BenRhouma/d9e42ca009a32f661b76 to your computer and use it in GitHub Desktop.
Save BenRhouma/d9e42ca009a32f661b76 to your computer and use it in GitHub Desktop.
public static string GenerateOctopusSlug(this string phrase)
{
string str = phrase.RemoveAccent().ToLower();
str = Regex.Replace(str, @"[\.+@$*%\^]", "-");
// 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", "-"); // replace spaces with hyphen
str = Regex.Replace(str, @"\-+", "-"); // remove multiple hyphens
str = Regex.Replace(str, @"\-*$", ""); // remove hyphens in the end of the string
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment