用正则表达式过滤HTML代码

 
2008-01-24 15:01

<%
Option Explicit
Function stripHTML(strHTML)
'Strips the HTML tags from strHTML

Dim objRegExp, strOutput ,db ,db1
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
%>

打开数据库记录集操作
<%
    dim rs,sql,iCount,iPageSize,maxpage,page,x,i
   set rs=server.createobject("adodb.recordset")
   sql = "select * from 表名 order by id desc"
   rs.open sql,conn,1,3
   %>

RS 输出内容(用LEN+LEFT截取数据)

<%if len(StripHTML(rs("读取的内容")))>100 then%>
<%=left(StripHTML(rs("读取的内容")),100)%>&nbsp;&nbsp;<a href="#">更多>></a>
<%else%>
<%=StripHTML(rs("读取的内容"))%>
<%
end if
%>

最需要主意的地方:
Option Explicit 语句

强制要求显式声明脚本中的所有变量。

如果使用 Option Explicit,该语句必须出现在脚本的任何其他语句之前。要放在首行。
必须使用 Dim、Private、Public 或 ReDim 语句显式声明所有变量。如果试图使用未经声明的变量名,则会出现错误。

看不明白的地方 可以加我Q(1593399)或给我留言!

用此正则表达式可以剔除用ASP编辑器添加数据时的HTML代码,因为在读取数据库时读取的HTML代码会影响到页面的显示效果。

原文地址:https://www.cnblogs.com/yeye518/p/2231791.html