当前位置:96看吧 > 技术文档 > ASP编程 > asp函数过滤HTML各种标签样式脚本

asp函数过滤HTML各种标签样式脚本

时间:2009/12/13
作者:jorkin
来源:jorkin.reallydo.com
查看:
标签:asp函数

<%
'过滤HTML各种标签样式脚本
'需要RegReplace函数

Function HTMLFilter(sHTML, sFilters)
If sHTML & "" = "" Then Exit Function
If sFilters & "" = "" Then sFilters = "SCRIPT,OBJECT"
Dim aFilters
aFilters = Split(UCase(sFilters), ",")
For i = 0 To UBound(aFilters)
Select Case UCase(Trim(aFilters(i)))
Case "JORKIN"
Do While InStr(sHTML, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") >0
sHTML = Replace(sHTML, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;&nbsp;&nbsp;")
Loop
Case "SCRIPT"
'// 去除脚本<scr ipt></scr ipt>及 onload 等
sHTML = RegReplace(sHTML, "<SCRIPT[\s\S]*?</SCRIPT>", "")
sHTML = RegReplace(sHTML, "\s[on].+?=\s+?([\""|\'])(.*?)\1", "")
sHTML = RegReplace(sHTML, "(JAVASCRIPT|JSCRIPT|VBSCRIPT|VBS):", "$1:")
Case "FIXIMG"
sHTML = RegReplace(sHTML, "<IMG.*?\sSRC=([^\""\'\s][^\""\'\s>]*).*?>", "<img src=$2 border=0>")
sHTML = RegReplace(sHTML, "<IMG.*SRC=([\""\']?)(.\1\S+).*?>", "<img src=$2 border=0>")
Case "TABLE"
'// 去除表格<table><tr><td><th>
sHTML = RegReplace(sHTML, "</?TABLE[^>]*>", "")
sHTML = RegReplace(sHTML, "</?TBODY[^>]*>", "")
sHTML = RegReplace(sHTML, "<(/?)TR[^>]*>", "<$1p>")
sHTML = RegReplace(sHTML, "</?TH[^>]*>", " ")
sHTML = RegReplace(sHTML, "</?TD[^>]*>", " ")
Case "CLASS"
'// 去除样式类class=""
sHTML = RegReplace(sHTML, "(<[^>]+) CLASS=[^ |^>]+([^>]*>)", "$1 $2")
sHTML = RegReplace(sHTML, "\sCLASS\s*?=\s*?([\""|\'])(.*?)\1", "")
Case "STYLE"
'// 去除样式style=""
sHTML = RegReplace(sHTML, "(<[^>]+) STYLE=[^ |^>]+([^>]*>)", "$1 $2")
sHTML = RegReplace(sHTML, "\sSTYLE\s*?=\s*?([\""|\'])(.*?)\1", "")
Case "XML"
'// 去除XML<?xml>
sHTML = RegReplace(sHTML, "<\\?XML[^>]*>", "")
Case "NAMESPACE"
'// 去除命名空间<o:p></o:p>
sHTML = RegReplace(sHTML, "<\/?[a-z]+:[^>]*>", "")
Case "FONT"
'// 去除字体<font></font>
sHTML = RegReplace(sHTML, "</?FONT[^>]*>", "")
Case "MARQUEE"
'// 去除字幕<marquee></marquee>
sHTML = RegReplace(sHTML, "</?MARQUEE[^>]*>", "")
Case "OBJECT"
'// 去除对象<object><param><embed></object>
sHTML = RegReplace(sHTML, "</?OBJECT[^>]*>", "")
sHTML = RegReplace(sHTML, "</?PARAM[^>]*>", "")
sHTML = RegReplace(sHTML, "</?EMBED[^>]*>", "")
Case "COMMENT"
'// 去除HTML注释, 会处理<script>和<style>内注释, 慎用
sHTML = RegReplace(sHTML, "<!--[\s\S]*?-->", "")
Case Else
'// 去除其它标签
sHTML = RegReplace(sHTML, "</?" & aFilters(i) & "[^>]*?>", "")
End Select
Next
HTMLFilter = sHTML
End Function
%>

 

<%
'功能:使用正则表示式对字符串进行替换

Function RegReplace(Str, PatternStr, RepStr)
    Dim NewStr, regEx
    NewStr = Str
    If IsNull(NewStr) Then
        RegReplace = ""
        Exit Function
    End If
    Set regEx = New RegExp
    regEx.IgnoreCase = True
    regEx.Global = True
    regEx.Pattern = PatternStr
    NewStr = regEx.Replace(NewStr, RepStr)
    RegReplace = NewStr
End Function
%>
 

上一篇:ASP函数ReplaceHTML()去掉全部HTML标记
下一篇:asp中rs(0)和rs("字段名")的执行效率
页面举报
Report
关闭页面
Close
收藏页面
Favorites
分享页面
Share
版权信息:栏目内,站内会员所分享的全部“资源/素材/文章”,仅供学习与参考,版权为原作者所有。
下载提示:非注册用户每天可下载一个文件,已注册会员不受限制。
网友评论
数据载入中
验证码
  • 请您注意:
  • ·请不要在评论中含与内容无关的广告链接。
  • ·不良评论请用报告管理员,以利管理员及时删除。
  • ·遵守中华人民共和国的各项有关法律法规
  • ·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • ·本站管理人员有权保留或删除评论中的任意内容
  • ·您在本站发表的作品,本站有权在网站内转载或引用
  • ·参与本评论即表明您已经阅读并接受上述条款
相关最新
相关热门