Asp过滤掉HTML标记方法汇总

'过滤掉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
 str=Replace(str,chr(10),"")
 str=Replace(str,chr(13),"")
 str=Replace(str," ","")
 str=Replace(str," ","")
 str=Replace(str,"&nbsp;","")
 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
End Function

 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

 '清梦的过滤HTML标记
Function RemoveHTML(strHTML)
 Dim objRegExp, Match, Matches
 Set objRegExp = New Regexp
 objRegExp.IgnoreCase = True
 objRegExp.Global = True
 objRegExp.Pattern = "<.+?>"     '取闭合的<>
 objRegExp.Pattern = "<img[\s\S]+>"    '过滤图片
 Set Matches = objRegExp.Execute(strHTML)  '进行匹配
 For Each Match in Matches      '遍历匹配集合,并替换掉匹配的项目
  strHtml=Replace(strHTML,Match.Value,"")
 Next
 RemoveHTML=strHTML
 Set objRegExp = Nothing
End Function

 '过滤所有HTML标记
Function DelHTML(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
 DelHTML = strTemp
End Function

 '过滤所有HTML标记
Function ClearHtml(str)  
 Dim re  
 Set re = new RegExp  
 re.IgnoreCase = True  
 re.Global = True  
 re.Pattern = "<(.[^>]*)>"  
 str = re.Replace(str,"")  
 set re = Nothing
 str=Replace(str,chr(10),"")
 str=Replace(str,chr(13),"")
 str=Replace(str," ","")
 str=Replace(str," ","")
 str=Replace(str,"&nbsp;","")
 ClearHtml = str
End Function

 Function LoseHtml(ContentStr)
 Dim ClsTempLoseStr,regEx
 ClsTempLoseStr = Cstr(ContentStr)
 Set regEx = New RegExp
 regEx.Pattern = "<\/*[^<>]*>"
 regEx.IgnoreCase = True
 regEx.Global = True
 ClsTempLoseStr = regEx.Replace(ClsTempLoseStr,"")
 LoseHtml = ClsTempLoseStr
End function

 '清除HTML标记
Function clearHTMLCode(originCode)
 Dim reg
 Set reg = new RegExp
 reg.Pattern = "<[^>]*>"
 reg.Global = True
 clearHTMLCode = reg.Replace(originCode, "")
End Function



上一篇: 正则表达式语法
下一篇: IE不支持Java?
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp
相关日志:
评论: 0 | 引用: 0 | 查看次数: 3430
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑