首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>ASP教程>资讯:ASP教程:过滤HTML代码并截取

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

www.jz123.cn  2010-01-14   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

  正常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 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
alert(nohtml("<b>jz123.cn</b><a href='http://www.jz123.cn'>中国建站</a>"))
</script>

  附带“过滤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


上一篇:学习ASP和编程的28个观点 下一篇:ASP编程常用的函数function大全

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图