Skip to content

Instantly share code, notes, and snippets.

@code-atom
Created March 1, 2017 10:23
Show Gist options
  • Save code-atom/830bdfe21b95ca18610ffdc04e85be9f to your computer and use it in GitHub Desktop.
Save code-atom/830bdfe21b95ca18610ffdc04e85be9f to your computer and use it in GitHub Desktop.
public static string GenerateSEOFriendlyUrl(string uncleanUrl)
{
//Trim Start and End Spaces.
uncleanUrl = uncleanUrl.Trim();
//Trim "-" Hyphen
uncleanUrl = uncleanUrl.Trim('-');
uncleanUrl = uncleanUrl.ToLower();
char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
//Replace . with - hyphen
uncleanUrl = uncleanUrl.Replace(".", "-");
//Replace Special-Characters
for (int i = 0; i < chars.Length; i++)
{
string strChar = chars.GetValue(i).ToString();
if (uncleanUrl.Contains(strChar))
uncleanUrl = uncleanUrl.Replace(strChar, string.Empty);
}
//Replace all spaces with one "-" hyphen
uncleanUrl = uncleanUrl.Replace(" ", "-");
//Replace multiple "-" hyphen with single "-" hyphen.
uncleanUrl = uncleanUrl.Replace("--", "-");
uncleanUrl = uncleanUrl.Replace("---", "-");
uncleanUrl = uncleanUrl.Replace("----", "-");
uncleanUrl = uncleanUrl.Replace("-----", "-");
uncleanUrl = uncleanUrl.Replace("----", "-");
uncleanUrl = uncleanUrl.Replace("---", "-");
uncleanUrl = uncleanUrl.Replace("--", "-");
//Run the code again...
//Trim Start and End Spaces.
uncleanUrl = uncleanUrl.Trim();
//Trim "-" Hyphen
uncleanUrl = uncleanUrl.Trim('-');
if (uncleanUrl.Length > 100)
{
uncleanUrl = uncleanUrl.Substring(0, 100);
}
return uncleanUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment