开发者

How to replace accented characters by their HTML representation

I would like to transform strings like 开发者_开发知识库"rég" to "grégou".

I temporarily wrote some code that manually changes the most common accents, but I would like to get one that transforms each accent to its html equivalent.

Someone has an idea? :)

ps: I tried something but it does not work ...

C # code:

public static MvcHtmlString MyEncode(this HtmlHelper htmlHelper, string text)
{
    StringBuilder builder = new StringBuilder();
    Byte[] bArray;


    HttpUtility.HtmlEncode(text);

    bArray = System.Text.Encoding.GetEncoding(850).GetBytes(text); 

    String chaine = "";


    for(int i=0; i<bArray.Length; i++)
    {
        chaine = chaine + (char)bArray[i];
    }

    HttpUtility.HtmlEncode(chaine);
    builder.Append(chaine);
    return MvcHtmlString.Create(builder.ToString());
}

--OLD


The HttpUtility.HtmlEncode Method does not modify the argument (strings in C# are immutable!); it returns the encoded version as a new string:

string encoded = HttpUtility.HtmlEncode("rég");

The preferred way to encode text in the context of MVC seems to be the Html.Encode Helper Method:

<%= Html.Encode("rég") %>


The library HelperSharp has a method for this purpose: EscapeAccentsToHtmlEntities

// The result will be: gr&eacute;gou
var escaped = "grégou".EscapeAccentsToHtmlEntities(); 


A quick google search for "HTML entity encode C#" brings up lots of hits ... like the following:

http://www.codeproject.com/KB/recipes/htmlencodingcsharp.aspx

There are also framework classes that perform this function:

http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜