求一个去除html代码的函数。

mezzi100 2006-07-04 11:07:22
看了不少函数,都没有完全解决问题,如下2则:
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

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
set Matches=nothing
End Function
因为我的html字符串是这样的:
<HEAD> <STYLE type=text/css>body,td,div,span{font-family:"宋体";font-size:12px;Color:#000000;}</STYLE> <META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD> <BODY> <P>长春到西安站的<FONT color=#0909f7>确切时间</FONT>是每天的中午14:0...

如上的字符串,上面的2个函数之能够去掉<..>标记里的数据,而不能去掉<..>body,td,div..</..>这种css样式的代码,请教高手解答?
...全文
264 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
keenx 2006-07-12
  • 打赏
  • 举报
回复
getText();
wj2100 2006-07-12
  • 打赏
  • 举报
回复
上面函数应该可以吧,我试试!
wcfboy1 2006-07-04
  • 打赏
  • 举报
回复
用RePlace 函数
yongfa365 2006-07-04
  • 打赏
  • 举报
回复
Function cutStr(str,strlen)
'去掉所有HTML标记
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
cutStr=str
Next
cutStr=Replace(cutStr,chr(10),"")
cutStr=Replace(cutStr,chr(13),"")
cutStr=Replace(cutStr,chr(32),"")
cutStr=Replace(cutStr," ","")
End Function
边城的刀声 2006-07-04
  • 打赏
  • 举报
回复
UP
mezzi100 2006-07-04
  • 打赏
  • 举报
回复
cestar2005的试过也不行。
mezzi100 2006-07-04
  • 打赏
  • 举报
回复
楼上你给的是js的,有没有asp里用的。
这个js我调不出来,insertHTML不知道是什么?
cestar2005 2006-07-04
  • 打赏
  • 举报
回复
Function ClearHtml(TestString)
Dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(\<.[^\<]*\>)"
TestString=re.replace(TestString,"")
re.Pattern="(\<\/[^\<]*\>)"
TestString=re.replace(TestString,"")
ClearHtml=TestString
Set re=Nothing
End Function
试试看
TSD 2006-07-04
  • 打赏
  • 举报
回复
你要清楚的是WORD的格式,当做HTML来做当然不行了.

这个应该对你有用,要做适当修改!

/ 清除WORD冗余格式并粘贴
function cleanAndPaste( html ) {
// Remove all SPAN tags
html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
// Remove Class attributes
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove Style attributes
html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
// Remove Lang attributes
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove XML elements and declarations
html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
// Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
// Replace the  
html = html.replace(/ /, " " );
// Transform <P> to <DIV>
var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ; // Different because of a IE 5.0 error
html = html.replace( re, "<div$2</div>" ) ;

insertHTML( html ) ;
}

28,402

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧