Skip to content

Instantly share code, notes, and snippets.

@dariuszparys
Created December 21, 2010 09:18
Show Gist options
  • Save dariuszparys/749696 to your computer and use it in GitHub Desktop.
Save dariuszparys/749696 to your computer and use it in GitHub Desktop.
WebMatrix Slug Helper
@using System.Text.RegularExpressions;
@*
Code by Kamran Ayub, from his post at
http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx
just added German special characters
*@
@helper Generate( string title, int maxLength = 50 )
{
var slug = title.ToLower();
slug = Regex.Replace( slug, "ä", "ae");
slug = Regex.Replace( slug, "ü", "ue");
slug = Regex.Replace( slug, "ö", "oe");
slug = Regex.Replace( slug, "ß", "ss");
slug = Regex.Replace( slug, @"[^a-z0-9\s-]", "");
slug = Regex.Replace( slug, @"[\s-]+", " ").Trim();
slug = slug.Substring( 0, slug.Length <= maxLength ? slug.Length : maxLength).Trim();
slug = Regex.Replace( slug, @"\s", "-");
@:@slug
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment