2011-05-24 40 views
8

Tôi đang cố chuyển đổi mã HTML như & #XXXX; (trong đó XXXX là một số) thành văn bản thuần túy bằng ASP cổ điển (VBScript).ASP cổ điển (VBScript) chuyển đổi mã HTML thành văn bản thuần túy

Tôi đang thêm văn bản vào email ở định dạng văn bản thuần túy và nếu tôi thêm chúng dưới dạng mã HTML, nó chỉ hiển thị mã và không chuyển đổi chúng.

Một sửa chữa sẽ là thay đổi email thành HTML để khắc phục sự cố đó nhưng sau đó gây ra các sự cố khác cho email của tôi mà tôi sẽ không tham gia.

Có chức năng tích hợp sẵn hoặc chức năng tùy chỉnh mà tôi có thể sử dụng để chuyển đổi các mã HTML này thành văn bản thuần túy không?

Trả lời

17

Những gì bạn cần là HTML Decode, mặc dù ASP không bao gồm ASP.

Chức năng này, được tìm thấy trên ASP Nut và được tôi sửa đổi rất nhiều, nên làm những gì bạn cần. Tôi đã thử nghiệm nó như vbscript chạy trên máy tính địa phương của tôi và nó có vẻ hoạt động tốt, ngay cả với các biểu tượng Unicode trong phạm vi 1000+.

Function HTMLDecode(sText) 
    Dim regEx 
    Dim matches 
    Dim match 
    sText = Replace(sText, """, Chr(34)) 
    sText = Replace(sText, "<" , Chr(60)) 
    sText = Replace(sText, ">" , Chr(62)) 
    sText = Replace(sText, "&" , Chr(38)) 
    sText = Replace(sText, " ", Chr(32)) 


    Set regEx= New RegExp 

    With regEx 
    .Pattern = "&#(\d+);" 'Match html unicode escapes 
    .Global = True 
    End With 

    Set matches = regEx.Execute(sText) 

    'Iterate over matches 
    For Each match in matches 
     'For each unicode match, replace the whole match, with the ChrW of the digits. 

     sText = Replace(sText, match.Value, ChrW(match.SubMatches(0))) 
    Next 

    HTMLDecode = sText 
End Function 

Lưu ý: Bạn sẽ cần kịch bản phiên bản 5.0 được cài đặt trên máy chủ của bạn để sử dụng đối tượng RegExp.

+0

Đã cố gắng mà một, Mã HTML mà tôi đang sử dụng là dành cho chữ Ả Rập nên mã là cái gì đó như ـ mà rõ ràng là ISN' t được bao phủ bởi từ 1 đến 255. –

+0

@Scrooby và 'Chr' không hỗ trợ mọi thứ trong phạm vi đó ... một chút tìm kiếm chỉ ra rằng chức năng' ChrW' thực hiện những gì bạn muốn với unicode. –

+0

Cảm ơn chức năng, tôi đang gặp một vài vấn đề với nó. Khi tôi thử và giải mã một trong các chữ cái Ả Rập, ví dụ: ا nó chỉ trả về dấu chấm hỏi. Tôi đoán nó là một cái gì đó để làm với bộ ký tự trang của tôi, mặc dù trang của tôi được thiết lập để ISO-8859-6 như là hình thức tôi đang cố gắng sử dụng giá trị in –

-1

Tôi đã thực hiện điều này dựa trên câu trả lời của C.Ross ở trên và một số mã từ một người khác đã thực hiện fb_app.inc. Nó sẽ làm các trick.

<% 
    Option Explicit 

    Dim objHelper 
    Set objHelper = New HtmlEntityToUnicode 

    Response.Write(objHelper.HtmlDecode("<br/><br/>hi &#128154; green heart! purple heart &#128156; ! ")) 

    Set objHelper = Nothing 

    Class HtmlEntityToUnicode 
    dim BITS_TO_A_BYTE 
    dim BYTES_TO_A_WORD 
    dim BITS_TO_A_WORD 
    Dim m_lOnBits(30) 
    Dim m_l2Power(30) 

    Sub Class_Initialize() 


     BITS_TO_A_BYTE = 8 
     BYTES_TO_A_WORD = 4 
     BITS_TO_A_WORD = 32 

     m_lOnBits(0) = CLng(1) 
     m_lOnBits(1) = CLng(3) 
     m_lOnBits(2) = CLng(7) 
     m_lOnBits(3) = CLng(15) 
     m_lOnBits(4) = CLng(31) 
     m_lOnBits(5) = CLng(63) 
     m_lOnBits(6) = CLng(127) 
     m_lOnBits(7) = CLng(255) 
     m_lOnBits(8) = CLng(511) 
     m_lOnBits(9) = CLng(1023) 
     m_lOnBits(10) = CLng(2047) 
     m_lOnBits(11) = CLng(4095) 
     m_lOnBits(12) = CLng(8191) 
     m_lOnBits(13) = CLng(16383) 
     m_lOnBits(14) = CLng(32767) 
     m_lOnBits(15) = CLng(65535) 
     m_lOnBits(16) = CLng(131071) 
     m_lOnBits(17) = CLng(262143) 
     m_lOnBits(18) = CLng(524287) 
     m_lOnBits(19) = CLng(1048575) 
     m_lOnBits(20) = CLng(2097151) 
     m_lOnBits(21) = CLng(4194303) 
     m_lOnBits(22) = CLng(8388607) 
     m_lOnBits(23) = CLng(16777215) 
     m_lOnBits(24) = CLng(33554431) 
     m_lOnBits(25) = CLng(67108863) 
     m_lOnBits(26) = CLng(134217727) 
     m_lOnBits(27) = CLng(268435455) 
     m_lOnBits(28) = CLng(536870911) 
     m_lOnBits(29) = CLng(1073741823) 
     m_lOnBits(30) = CLng(2147483647) 

     m_l2Power(0) = CLng(1) 
     m_l2Power(1) = CLng(2) 
     m_l2Power(2) = CLng(4) 
     m_l2Power(3) = CLng(8) 
     m_l2Power(4) = CLng(16) 
     m_l2Power(5) = CLng(32) 
     m_l2Power(6) = CLng(64) 
     m_l2Power(7) = CLng(128) 
     m_l2Power(8) = CLng(256) 
     m_l2Power(9) = CLng(512) 
     m_l2Power(10) = CLng(1024) 
     m_l2Power(11) = CLng(2048) 
     m_l2Power(12) = CLng(4096) 
     m_l2Power(13) = CLng(8192) 
     m_l2Power(14) = CLng(16384) 
     m_l2Power(15) = CLng(32768) 
     m_l2Power(16) = CLng(65536) 
     m_l2Power(17) = CLng(131072) 
     m_l2Power(18) = CLng(262144) 
     m_l2Power(19) = CLng(524288) 
     m_l2Power(20) = CLng(1048576) 
     m_l2Power(21) = CLng(2097152) 
     m_l2Power(22) = CLng(4194304) 
     m_l2Power(23) = CLng(8388608) 
     m_l2Power(24) = CLng(16777216) 
     m_l2Power(25) = CLng(33554432) 
     m_l2Power(26) = CLng(67108864) 
     m_l2Power(27) = CLng(134217728) 
     m_l2Power(28) = CLng(268435456) 
     m_l2Power(29) = CLng(536870912) 
     m_l2Power(30) = CLng(1073741824) 

    End Sub 

    Public Function HTMLDecode(sText) 
     Dim regEx 
     Dim matches 
     Dim match 
     sText = Replace(sText, "&quot;", Chr(34)) 
     sText = Replace(sText, "&lt;" , Chr(60)) 
     sText = Replace(sText, "&gt;" , Chr(62)) 
     sText = Replace(sText, "&amp;" , Chr(38)) 
     sText = Replace(sText, "&nbsp;", Chr(32)) 


     Set regEx= New RegExp 

     With regEx 
     .Pattern = "&#(\d+);" 'Match html unicode escapes 
     .Global = True 
     End With 

     Set matches = regEx.Execute(sText) 

     'Iterate over matches 
     For Each match in matches 
     'For each unicode match, replace the whole match, with the ChrW of the digits. 
      sText = Replace(sText, match.Value, "\U"&WordToHex(match.SubMatches(0))) 
     Next 

     HTMLDecode = sText 
    End Function 


    Private Function WordToHex(lValue) 
     Dim lByte 
     Dim lCount 

     For lCount = 0 To 3 
      lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1) 
      WordToHex = Right("0" & Hex(lByte), 2) & WordToHex 
     Next 
    End Function 

    Private Function RShift(lValue, iShiftBits) 
     If iShiftBits = 0 Then 
      RShift = lValue 
      Exit Function 
     ElseIf iShiftBits = 31 Then 
      If lValue And &H80000000 Then 
       RShift = 1 
      Else 
       RShift = 0 
      End If 
      Exit Function 
     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then 
      Err.Raise 6 
     End If 

     RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits) 

     If (lValue And &H80000000) Then 
      RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1))) 
     End If 
    End Function 

    End Class 


    %> 
0

Một bộ giải mã exausted hơn epanding C. Ross phản ứng

Function HTMLDecode(sText) 
    Dim regEx 
    Dim matches 
    Dim match 
    sText = Replace(sText, "&quot;", Chr(34)) 
    sText = Replace(sText, "&lt;" , Chr(60)) 
    sText = Replace(sText, "&gt;" , Chr(62)) 
    sText = Replace(sText, "&amp;" , Chr(38)) 
    sText = Replace(sText, "&nbsp;", Chr(32)) 

    sText = Replace(sText, "&iexcl;", Chr(161)) 
    sText = Replace(sText, "&pound;", Chr(163)) 
    sText = Replace(sText, "&yen;", Chr(165)) 
    sText = Replace(sText, "&copy;", Chr(168)) 
    sText = Replace(sText, "&laquo;", Chr(171)) 
    sText = Replace(sText, "&raquo;", Chr(187)) 
    sText = Replace(sText, "&iquest;", Chr(191)) 
    sText = Replace(sText, "&Agrave;", Chr(192)) 
    sText = Replace(sText, "&Aacute;", Chr(193)) 
    sText = Replace(sText, "&Acirc;", Chr(194)) 
    sText = Replace(sText, "&Atilde;", Chr(195)) 
    sText = Replace(sText, "&Auml;", Chr(196)) 
    sText = Replace(sText, "&Aring;", Chr(197)) 
    sText = Replace(sText, "&AElig;", Chr(198)) 
    sText = Replace(sText, "&Ccedil;", Chr(199)) 
    sText = Replace(sText, "&Egrave;", Chr(200)) 
    sText = Replace(sText, "&Eacute;", Chr(201)) 
    sText = Replace(sText, "&Ecirc;", Chr(202)) 
    sText = Replace(sText, "&Euml;", Chr(203)) 
    sText = Replace(sText, "&Igrave;", Chr(204)) 
    sText = Replace(sText, "&Iacute;", Chr(205)) 
    sText = Replace(sText, "&Icirc;", Chr(206)) 
    sText = Replace(sText, "&Iuml;", Chr(207)) 
    sText = Replace(sText, "&Ntilde;", Chr(209)) 
    sText = Replace(sText, "&Ograve;", Chr(210)) 
    sText = Replace(sText, "&Oacute;", Chr(211)) 
    sText = Replace(sText, "&Ocirc;", Chr(212)) 
    sText = Replace(sText, "&Otilde;", Chr(213)) 
    sText = Replace(sText, "&Ouml;", Chr(214)) 
    sText = Replace(sText, "&times;", Chr(215)) 
    sText = Replace(sText, "&Oslash;", Chr(216)) 
    sText = Replace(sText, "&Ugrave;", Chr(217)) 
    sText = Replace(sText, "&Uacute;", Chr(218)) 
    sText = Replace(sText, "&Ucirc;", Chr(219)) 
    sText = Replace(sText, "&Uuml;", Chr(220)) 
    sText = Replace(sText, "&Yacute;", Chr(221)) 
    sText = Replace(sText, "&THORN;", Chr(222)) 
    sText = Replace(sText, "&szlig;", Chr(223)) 
    sText = Replace(sText, "&agrave;", Chr(224)) 
    sText = Replace(sText, "&aacute;", Chr(225)) 
    sText = Replace(sText, "&acirc;", Chr(226)) 
    sText = Replace(sText, "&atilde;", Chr(227)) 
    sText = Replace(sText, "&auml;", Chr(228)) 
    sText = Replace(sText, "&aring;", Chr(229)) 
    sText = Replace(sText, "&aelig;", Chr(230)) 
    sText = Replace(sText, "&ccedil;", Chr(231)) 
    sText = Replace(sText, "&egrave;", Chr(232)) 
    sText = Replace(sText, "&eacute;", Chr(233)) 
    sText = Replace(sText, "&ecirc;", Chr(234)) 
    sText = Replace(sText, "&euml;", Chr(235)) 
    sText = Replace(sText, "&igrave;", Chr(236)) 
    sText = Replace(sText, "&iacute;", Chr(237)) 
    sText = Replace(sText, "&icirc;", Chr(238)) 
    sText = Replace(sText, "&iuml;", Chr(239)) 
    sText = Replace(sText, "&eth;", Chr(240)) 
    sText = Replace(sText, "&ntilde;", Chr(241)) 
    sText = Replace(sText, "&ograve;", Chr(242)) 
    sText = Replace(sText, "&oacute;", Chr(243)) 
    sText = Replace(sText, "&ocirc;", Chr(244)) 
    sText = Replace(sText, "&otilde;", Chr(245)) 
    sText = Replace(sText, "&ouml;", Chr(246)) 
    sText = Replace(sText, "&divide;", Chr(247)) 
    sText = Replace(sText, "&oslash;", Chr(248)) 
    sText = Replace(sText, "&ugrave;", Chr(249)) 
    sText = Replace(sText, "&uacute;", Chr(250)) 
    sText = Replace(sText, "&ucirc;", Chr(251)) 
    sText = Replace(sText, "&uuml;", Chr(252)) 
    sText = Replace(sText, "&yacute;", Chr(253)) 
    sText = Replace(sText, "&thorn;", Chr(254)) 
    sText = Replace(sText, "&yuml;", Chr(255)) 

    Set regEx= New RegExp 

    With regEx 
    .Pattern = "&#(\d+);" 'Match html unicode escapes 
    .Global = True 
    End With 

    Set matches = regEx.Execute(sText) 

    'Iterate over matches 
    For Each match in matches 
     'For each unicode match, replace the whole match, with the ChrW of the digits. 

     sText = Replace(sText, match.Value, ChrW(match.SubMatches(0))) 
    Next 

    HTMLDecode = sText 
End Function 
Các vấn đề liên quan