Skip to content

Instantly share code, notes, and snippets.

@paracycle
Forked from onebeatconsumer/sluggenerator.cs
Created November 30, 2012 18:24
Show Gist options
  • Save paracycle/4177552 to your computer and use it in GitHub Desktop.
Save paracycle/4177552 to your computer and use it in GitHub Desktop.
Slug Generator - C# - String Extension Method
/// <summary>
/// Generates a permalink slug for passed string
/// </summary>
/// <param name="phrase"></param>
/// <returns>clean slug string (ex. "some-cool-topic")</returns>
public static string GenerateSlug(this string phrase)
{
var s = phrase.RemoveAccent().ToLower();
s = Regex.Replace(s, @"[^a-z0-9\s-]", ""); // remove invalid characters
s = Regex.Replace(s, @"\s+", " ").Trim(); // single space
s = s.Substring(0, s.Length <= 45 ? s.Length : 45).Trim(); // cut and trim
s = Regex.Replace(s, @"\s", "-"); // insert hyphens
return s.ToLower();
}
@alebrozzoSP
Copy link

You are missing the definition of RemoveAccent

@phi-dev
Copy link

phi-dev commented Dec 31, 2017

#alebrozzoSP
RemoveAccent method:
public static string RemoveAccent(this string str)
{
var bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(str);
return System.Text.Encoding.ASCII.GetString(bytes);
}

@kiaksarg
Copy link

kiaksarg commented May 24, 2019

Persian support:

 /// <summary>
        /// Generates a permalink slug for passed string
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns>clean slug string (ex. "some-cool-topic")</returns>
        public static string GenerateSlug(this string phrase)
        {
            var s = phrase.RemoveDiacritics().ToLower();
            s = Regex.Replace(s, @"[^\u0600-\u06FF\uFB8A\u067E\u0686\u06AF\u200C\u200Fa-z0-9\s-]", "");                      // remove invalid characters
            s = Regex.Replace(s, @"\s+", " ").Trim();                       // single space
            s = s.Substring(0, s.Length <= 100 ? s.Length : 45).Trim();      // cut and trim
            s = Regex.Replace(s, @"\s", "-");                               // insert hyphens        
            s = Regex.Replace(s, @"‌", "-");                                // half space
            return s.ToLower();
        }

        public static string RemoveDiacritics(this string text)
        {
            if (string.IsNullOrWhiteSpace(text))
                return string.Empty;

            var normalizedString = text.Normalize(NormalizationForm.FormKC);
            var stringBuilder = new StringBuilder();

            foreach (var c in normalizedString)
            {
                var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
                if (unicodeCategory != UnicodeCategory.NonSpacingMark)
                {
                    stringBuilder.Append(c);
                }
            }

            return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
        }


         //Tests
        //[DataRow("برگی از ادبیات زبان فارسی", "برگی-از-ادبیات-زبان-فارسی")]
        //[DataRow("برگی از ادبیات پژگچ زبان فارسی", "برگی-از-ادبیات-پژگچ-زبان-فارسی")]
        //[DataRow("  برگی از ادبیات/// / زبان فارسی   ", "برگی-از-ادبیات-زبان-فارسی")]
        //[DataRow("برگی از ادبیات‌زبان فارسی", "برگی-از-ادبیات-زبان-فارسی")]
        //[DataRow("برگی             از        ادبیات    زبان            فارسی", "برگی-از-ادبیات-زبان-فارسی")]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment