asp中去除内容HTML标签的三个function函数

2018-09-06 11:56

阅读:356

  复制代码 代码如下:
==============================
功能描述:用正则除去HTML标记
不能保留<b><strong>等以及用户自定义的<和>
==============================

FunctionRemoveHTMLTag(fString)
Dimre
Setre=NewRegExp
re.IgnoreCase=True
re.Pattern=<(.[^>]*)>
fString=re.Replace(fString,)
Setre=Nothing
RemoveHTMLTag=fString
EndFunction

==============================
功能描述:除去HTML标记
不能保留<b><strong>等以及用户自定义的<和>
==============================

FunctionRemove_HTML(Str)
Dimilen1,ilen2
DoWhileInStr(1,Str,<,1)>0
ilen1=InStr(1,Str,<,1)
ilen2=InStr(1,Str,>,1)
Str=Left(Str,ilen1-1)&Mid(Str,ilen2+1)
Loop
Remove_HTML=Str
EndFunction

==============================
功能描述:除去HTML标记
去除自定义的标记,速度可能有点慢
==============================

FunctionRemoveHTML(strText)
DimTAGLIST
TAGLIST=;!--;!DOCTYPE;A;ACRONYM;ADDRESS;APPLET;AREA;B;BASE;BASEFONT;&_
BGSOUND;BIG;BLOCKQUOTE;BODY;BR;BUTTON;CAPTION;CENTER;CITE;CODE;&_
COL;COLGROUP;COMMENT;DD;DEL;DFN;DIR;DIV;DL;DT;EM;EMBED;FIELDSET;&_
FONT;FORM;FRAME;FRAMESET;HEAD;H1;H2;H3;H4;H5;H6;HR;HTML;I;IFRAME;IMG;&_
INPUT;INS;ISINDEX;KBD;LABEL;LAYER;LAGEND;LI;LINK;LISTING;MAP;MARQUEE;&_
MENU;META;NOBR;NOFRAMES;NOSCRIPT;OBJECT;OL;OPTION;P;PARAM;PLAINTEXT;&_
PRE;Q;S;SAMP;SCRIPT;Select;SMALL;SPAN;STRIKE;STRONG;STYLE;SUB;SUP;&_
TABLE;TBODY;TD;TEXTAREA;TFOOT;TH;THEAD;TITLE;TR;TT;U;UL;VAR;WBR;XMP;

ConstBLOCKTAGLIST=;APPLET;EMBED;FRAMESET;HEAD;NOFRAMES;NOSCRIPT;OBJECT;SCRIPT;STYLE;

DimnPos1
DimnPos2
DimnPos3
DimstrResult
DimstrTagName
DimbRemove
DimbSearchForBlock

nPos1=InStr(strText,<)
DoWhilenPos1>0
nPos2=InStr(nPos1+1,strText,>)
IfnPos2>0Then
strTagName=Mid(strText,nPos1+1,nPos2-nPos1-1)
strTagName=Replace(Replace(strTagName,vbCr,),vbLf,)

nPos3=InStr(strTagName,)
IfnPos3>0Then
strTagName=Left(strTagName,nPos3-1)
EndIf


IfLeft(strTagName,1)=/Then
strTagName=Mid(strTagName,2)
bSearchForBlock=False
Else
bSearchForBlock=True
EndIf

IfInStr(1,TAGLIST,;&strTagName&;,vbTextCompare)>0Then
bRemove=True
IfbSearchForBlockThen
IfInStr(1,BLOCKTAGLIST,;&strTagName&;,vbTextCompare)>0Then
nPos2=Len(strText)
nPos3=InStr(nPos1+1,strText,</&strTagName,vbTextCompare)
IfnPos3>0Then
nPos3=InStr(nPos3+1,strText,>)
EndIf

IfnPos3>0Then
nPos2=nPos3
EndIf
EndIf
EndIf
Else
bRemove=False
EndIf

IfbRemoveThen
strResult=strResult&Left(strText,nPos1-1)
strText=Mid(strText,nPos2+1)
Else
strResult=strResult&Left(strText,nPos1)
strText=Mid(strText,nPos1+1)
EndIf
Else
strResult=strResult&strText
strText=
EndIf

nPos1=InStr(strText,<)
Loop
strResult=strResult&strText
strResult=Replace(strResult,Chr(9),)
strResult=Replace(strResult,Chr(32),)
strResult=Replace(strResult,Chr(13),)
strResult=Replace(strResult,Chr(10),)
strResult=Replace(strResult,vbCrLf,)
RemoveHTML=strResult
EndFunction


评论


亲,登录后才可以留言!