Search code examples
encodingutf-8asp-classichtml-encode

How to stop Server.HtmlEncode to encode UTF8 characters?


How can I stop Server.HtmlEncode to encode UTF8 characters? I set the codepage to UTF8 but didn't help

Here is my test case:

<%@CODEPAGE=65001%>
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
%>
<%=Server.HtmlEncode("русский stuff <b>bold stuff</b>")%>

It should normally output this:

русский stuff &lt;b&gt;bold stuff&lt;/b&gt;

but the output is:

&#1088;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081; stuff &lt;b&gt;bold stuff&lt;/b&gt;

Solution

  • Server.HtmlEncode method escapes >,<,&," chars and any ascii code character whose code is greater than or equal to 0x80.
    You can filter which character will be escaped.
    There are generic characters will be encoded in pattern.
    If you prefer, you can add some other chars too.

    Private Function cb_Escape(ByVal a, ByVal b, ByVal c, ByVal d)
        cb_Escape = Server.HTMLEncode(b)
    End Function
    
    Private Function HTMLEncode2(ByVal sHTML)
        Dim oReg
        Set oReg = New RegExp
            oReg.Global = True
            oReg.Pattern = "([<>""&]+)"
            HTMLEncode2 = oReg.Replace(sHTML, GetRef("cb_Escape"))
        Set oReg = Nothing
    End Function
    
    Response.Write HTMLEncode2("русский stuff <b>bold stuff</b>")