URL Slugify算法在C#中? [英] URL Slugify algorithm in C#?

查看:109
本文介绍了URL Slugify算法在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已搜查,并通过标签上SO浏览,只发现了两个引人注目的解决方案:

So I have searched and browsed through the slug tag on SO and only found two compelling solution:

  • Slugify and Character Transliteration in C#
  • How to convert super- or subscript to normal text in C#

哪些但部分解决此问题。我可以手动code这件事自己,但我很惊讶,没有一个已经解掉那里。

Which are but partial solution to the problem. I could manually code this up myself but I'm surprised that there isn't already a solution out there yet.

那么,有没有在C#和/或.NET妥善处理拉丁字符,UNI code和其他各种语言问题是否正确?

So, is there a slugify alrogithm implementation in C# and/or .NET that properly address latin characters, unicode and various other language issues properly?

推荐答案

<一个href=\"http://$p$pdicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html\">http://$p$pdicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html

public static string GenerateSlug(this string phrase) 
{ 
    string str = phrase.RemoveAccent().ToLower(); 
    // 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", "-"); // hyphens   
    return str; 
} 

public static string RemoveAccent(this string txt) 
{ 
    byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt); 
    return System.Text.Encoding.ASCII.GetString(bytes); 
}

这篇关于URL Slugify算法在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆