简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM