Created
December 21, 2010 09:18
-
-
Save dariuszparys/749696 to your computer and use it in GitHub Desktop.
WebMatrix Slug Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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