几种过滤html代码的方法总结

http://hi.baidu.com/crp8/blog/item/b5d7e503fd35d9743812bb60.html

过滤html代码的sql自定义函数

create function yanzi 

@vstrIn varchar(1000) 

returns varchar(4000) 
as 
begin 
declare @strReturn varchar(4000) 
,@str varchar(4000) 
,@stmp varchar(1) 
,@i int 
,@len int

set @strReturn='' 
select @str=stuff(@str,1,2,'') 
set @len=len(@str) 
set @i=1

while @i<@len 
begin 
set @stmp = substring(@str,@i,1) 
set @stmp = replace(@stmp,'<','<') 
set @stmp = replace(@stmp,'>','>') 
set @strReturn = @strReturn + @stmp 
set @i=@i+1 
end 
return @strReturn 
end

用正则表达式过滤html代码

代码例子如下:
<%
Option Explicit

Function stripHTML(strHTML)
'Strips the HTML tags from strHTML

Dim objRegExp, strOutput
Set objRegExp = New Regexp

objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<.+?>"

'Replace all HTML tag matches with the empty string
strOutput = objRegExp.Replace(strHTML, "")
'Replace all < and > with < and >
strOutput = Replace(strOutput, "<", "<")
strOutput = Replace(strOutput, ">", ">")
stripHTML = strOutput 'Return the value of strOutput

Set objRegExp = Nothing
End Function
%>

<form method="post" id=form1 name=form1>
<b>Enter an HTML String:</b><br>
<textarea name="txtHTML" cols="50" rows="8" wrap="virtual"><%=Request("txtHTML")%></textarea>
<p>
<input type="submit" value="Strip HTML Tags!" id=submit1 name=submit1>
</form>

<% if Len(Request("txtHTML")) > 0 then %>
<p><hr><p>
<b><u>View of string <i>with no</i> HTML stripping:</u></b><br>
<xmp>
<%=Request("txtHTML")%>
</xmp><p>
<b><u>View of string <i>with</i> HTML stripping:</u></b><br>
<pre>
<%=StripHTML(Request("txtHTML"))%>
</pre>
<% End If %>

过滤html代码的函数

1、
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

2、
Function RemoveHTML(strHTML) 
Dim objRegExp, Match, Matches 
Set objRegExp = New Regexp 
objRegExp.IgnoreCase = True 
objRegExp.Global = True 
'取闭合的<> 
objRegExp.Pattern = "<.+?>" 
'进行匹配 
Set Matches = objRegExp.Execute(strHTML) 
' 遍历匹配集合,并替换掉匹配的项目 
For Each Match in Matches 
strHtml=Replace(strHTML,Match.Value,"") 
Next 
RemoveHTML=strHTML 
Set objRegExp = Nothing 
End Function

Rem 过滤HTML代码

function HTMLEncode(fString)
if not isnull(fString) then
     fString = replace(fString, ">", "&gt;")
     fString = replace(fString, "<", "&lt;")

     fString = Replace(fString, CHR(32), "&nbsp;")
     fString = Replace(fString, CHR(9), "&nbsp;")
     fString = Replace(fString, CHR(34), "&quot;")
     fString = Replace(fString, CHR(39), "&#39;")
     fString = Replace(fString, CHR(13), "")
     fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
     fString = Replace(fString, CHR(10), "<BR> ")

     'fString=ChkBadWords(fString)
     HTMLEncode = fString
else
    HTMLEncode=fstring
end if
end function

Rem 过滤SQL非法字符

function checkStr(str)
if isnull(str) then
   checkStr = ""
   exit function 
end if
checkStr=replace(str,"'","''")
end function

Rem 判断数字是否整形

function isInteger(para)
        on error resume next
        dim str
        dim l,i
        if isNUll(para) then 
           isInteger=false
           exit function
        end if
        str=cstr(para)
        if trim(str)="" then
           isInteger=false
           exit function
        end if
        l=len(str)
        for i=1 to l
            if mid(str,i,1)>"9" or mid(str,i,1)<"0" then
               isInteger=false 
               exit function
            end if
        next
        isInteger=true
        if err.number<>0 then err.clear
end function

Rem 过滤HTML

Function FilterHTML(strToFilter)
   Dim strTemp
   strTemp = strToFilter
   While Instr(1,strTemp,"<") AND Instr(1, strTemp, ">")
     strTemp = Left(strTemp, Instr(1, strTemp, "<")-1) & Right(strTemp, Len(strTemp)-Instr(1,strTemp, ">"))
   WEnd
   FilterHTML = strTemp
End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值