经典 ASP (VBScript) 将 HTML 代码转换为纯文本 [英] Classic ASP (VBScript) convert HTML codes to plain text

查看:30
本文介绍了经典 ASP (VBScript) 将 HTML 代码转换为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换像 &#XXXX; 这样的 HTML 代码.(其中 XXXX 是数字)使用经典 ASP (VBScript) 转换为纯文本.

I'm trying to convert HTML Codes like the &#XXXX; (where XXXX is a number) to plain text using classic ASP (VBScript).

我将文本添加到纯文本格式的电子邮件中,如果我将它们添加为 HTML 代码,它只会显示代码而不转换它们.

I'm adding the text to an email which is in plain text format and if I add them as HTML Codes, it just displays the code and doesn't convert them.

一种解决方法是将电子邮件更改为 HTML 格式,这确实解决了该问题,但随后会导致我的电子邮件出现其他问题,我不会讨论这些问题.

One fix would be to change the email to be HTML which does fix that problem but then causes other problems for my email which I won't go into.

是否有内置函数或自定义函数可以将这些 HTML 代码转换为纯文本?

Is there a built in function or a custom function I can use to convert these HTML Codes to plain text?

推荐答案

您需要的是 HTML 解码,但不幸的是 ASP 不包含.

What you need is HTML Decode, though unfortunately ASP doesn't include one.

这个函数是在 ASP Nut 上找到的,经过我的大量修改,应该可以满足您的需求.我将它作为在本地计算机上运行的 vbscript 进行了测试,它似乎运行良好,即使使用 1000+ 范围内的 Unicode 符号也是如此.

This function, found on ASP Nut, and modified heavily by me, should do what you need. I tested it as vbscript running on my local computer and it seemed to work well, even with Unicode symbols in the 1000+ range.

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

注意:您需要在服务器上安装脚本版本 5.0 才能使用 RegExp 对象.

这篇关于经典 ASP (VBScript) 将 HTML 代码转换为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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