作业帮 > ASP > 教育资讯

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

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/03 09:09:38 ASP
ASP教程:过滤HTML代码并截取
ASP教程:过滤HTML代码并截取ASP
【无忧考网-ASP教程:过滤HTML代码并截取】:
正常ASP中对录入内容的过滤仅仅是对左尖括号(<)和右尖括号的(>)的HTML源码的替换,以在页面中显示为左右尖括号,而不是将尖括号作为HTML标签执行了。

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

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


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


做过简单的应用。



 [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," ","") 
End Function 


ASP