ASP教程:过滤HTML代码并截取

来源:互联网 发布:js 日期 时间选择插件 编辑:程序博客网 时间:2024/05/21 17:51

正常ASP中对录入内容的过滤仅仅是对左尖括号(<)和右尖括号的(>)的HTML源码的替换,以在页面中显示为左右尖括号,而不是将尖括号作为HTML标签执行了。

当然这应该是属于正常过滤手法,而还有一种过滤方法则是将一对尖括号及尖括号中的所有字符均替换不显示,作为过滤HTML标签的最终极手法,但该方法对于内容中必须描述有关尖括号内容则是过滤过头了(比如本文起始所引用的相关尖括号符号均将看不见“<>”,文章中用来描述HTML标签“<hr>”也将看不见)。

不过,总归是有需要将所有尖括号中内容全部替换的时候。很显然是需要进行正则的。搜索了一段代码

Function noHTML(str)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(/<.[^/<]*/>)"
str=re.replace(str,"")
re.Pattern="(/<//[^/<]*/>)"
str=re.replace(str,"")
nohtml=str
set re=nothing
End Function

或者

Function noHTML(str)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="<(.[^>]*)>"
str=re.replace(str,"")
nohtml=str
set re=nothing
End Function

做过简单的应用。

<script language=vbs>
Function no<a href="http://www.blue1000.com/bkhtml/c22/" title="HTML/JavaScript教程">HTML</a>(str)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="<(.[^>]*)>"
str=re.replace(str,"")
nohtml=str
set re=nothing
End Function
alert(nohtml("<b>webjx.com</b><a href=http://www.webyi.com/wschool/program/<a href="http://www.blue1000.com/bkhtml/c16/" title="ASP教程">ASP</a>/20081214/'http://www.webyi.com'>网翼教程网</a>"))
</script>

 [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

附带“过滤HTML代码并截取”的代码

Function cutStr(str,strlen)
Dim re
Set re=new RegExp
re.IgnoreCase =True
re.Global=True
re.Pattern="<(.[^>]*)>"
str=re.Replace(str,"")
set re=Nothing
Dim l,t,c,i
l=Len(str)
t=0
For i=1 to l
c=Abs(Asc(Mid(str,i,1)))
If c>255 Then
t=t+2
Else
t=t+1
End If
If t>=strlen Then
cutStr=left(str,i)&"..."
Exit For
Else
cutStr=str
End If
Next
cutStr=Replace(cutStr,chr(10),"")
cutStr=Replace(cutStr,chr(13)," ")
cutStr=Replace(cutStr,"&nbsp;","")
End Function

 

原创粉丝点击