栏目导航
热点推荐
- ASP下批量删除数据的两种方法
- 关于二级域名共享cookies
- ASP错误代码大全
- ASP代码实现show.asp?id=26变成s
- ASP错误信息解决:IIS启用父路径
- 一些常用的正则表达式大全
- ASP超级链接和HTML函数正则表达
- ASP隐藏下载地址及防盗代码
- 最常用的正则表达式示例
- ASP程序数据库被挂马的处理方法
- ASP生成html的新方法
- 精通ASP错误提示大全
阅览排行
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," ","") End Function |
1
上一篇:学习ASP和编程的28个观点 下一篇:ASP编程常用的函数function大全