好东西,老外用正则表达式写的HTML分离函数
程序员文章站
2022-03-03 11:45:36
存成.asp文件,执行,你用asphttp抓内容的时候用这个很爽,当然自己要改进一下了<%option explicitfunction striphtml(strh...
存成.asp文件,执行,你用asphttp抓内容的时候用这个很爽,当然自己要改进一下了
<%
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 %>
<%
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 %>
上一篇: 学习网址