ClassicASP 모든 태그 제거 & 허용태그 외의 모든 html 태그 제거
페이지 정보
본문
<%
'// ======================================================
'// html 태그 속성 제거
'// 이용방법
'// content = "....."
'// contents = RemoveHTMLAttributes(contents)
'// ======================================================
function RemoveHTMLAttributes(strText)
set rex = new Regexp
rex.Pattern= "<[^>]+>"
rex.Global=true
strText=rex.Replace(strText,"")
strText=Replace(strText,"·","·")
RemoveHTMLAttributes=Replace(strText," "," ")
end function
'// ======================================================
'// HTML 태그 제거
'// 이용방법
'// content = "....."
'// contents = RemoveHTML(contents)
'// http://www.codeproject.com/Articles/639/Removing-HTML-from-the-text-in-ASP
'// ======================================================
Function RemoveHTML(strText)
Dim TAGLIST
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;"
Const BLOCKTAGLIST = ";APPLET;EMBED;FRAMESET;HEAD;NOFRAMES;NOscRIPT;OBJECT;scRIPT;STYLE;"
Dim nPos1, nPos2, nPos3, strResult, strTagName, bRemove, bSearchForBlock
nPos1 = InStr(strText, "<")
Do While nPos1 > 0
nPos2 = InStr(nPos1 + 1, strText, ">")
If nPos2 > 0 Then
strTagName = Mid(strText, nPos1 + 1, nPos2 - nPos1 - 1)
strTagName = Replace(Replace(strTagName, vbCr, " "), vbLf, " ")
nPos3 = InStr(strTagName, " ")
If nPos3 > 0 Then
strTagName = Left(strTagName, nPos3 - 1)
End If
If Left(strTagName, 1) = "/" Then
strTagName = Mid(strTagName, 2)
bSearchForBlock = False
Else
bSearchForBlock = True
End If
If InStr(1, TAGLIST, ";" & strTagName & ";", vbTextCompare) > 0 Then
bRemove = True
If bSearchForBlock Then
If InStr(1, BLOCKTAGLIST, ";" & strTagName & ";", vbTextCompare) > 0 Then
nPos2 = Len(strText)
nPos3 = InStr(nPos1 + 1, strText, "</" & strTagName, vbTextCompare)
If nPos3 > 0 Then
nPos3 = InStr(nPos3 + 1, strText, ">")
End If
If nPos3 > 0 Then
nPos2 = nPos3
End If
End If
End If
Else
bRemove = False
End If
If bRemove Then
strResult = strResult & Left(strText, nPos1 - 1)
strText = Mid(strText, nPos2 + 1)
Else
strResult = strResult & Left(strText, nPos1)
strText = Mid(strText, nPos1 + 1)
End If
Else
strResult = strResult & strText
strText = ""
End If
nPos1 = InStr(strText, "<")
Loop
strResult = strResult & strText
RemoveHTML = strResult
End Function
%>
< 이용 방법 >
<%
contents = RemoveHTML(contents)
%>
<%
'// ======================================================
'// 패턴으로 치환할수 있는 eregi_replace()함수
'// PHP에는 있으나 ASP에는 없기 때문
'// ======================================================
Function eregi_replace(pattern, replace, text)
Dim eregObj
Set eregObj = New RegExp
eregObj.Pattern = pattern '패턴 설정
eregObj.IgnoreCase = True '대소문자 구분 여부
eregObj.Global = True '전체 문서에서 검색
eregi_replace = eregObj.Replace(text, replace) 'Replace String
End Function
'// ======================================================
'// 모든 태그제거
'// 사용법 : strip_tags1(content)
'// content = "....."
'// strip_tags1(content)
'// ======================================================
Function strip_tags1(str)
Dim content
content = str
content = eregi_replace("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>","",content) ' all
content = eregi_replace("<(no)?script[^>]*>.*?</(no)?script>","",content) ' scripts
content = eregi_replace("<style[^>]*>.*</style>", "", content) ' style
'content = eregi_replace("<span[^>]*>.*</span>", "", content) ' span
content = eregi_replace("<(\""[^\""]*\""|\'[^\']*\'|[^\'\"">])*>","",content) ' TAGS
content = eregi_replace("<\\w+\\s+[^<]*\\s*>","",content) ' nTAGS
'content = eregi_replace("&[^;]+;","",content) ' entity_refs
content = eregi_replace("[^;]+;","",content) ' entity_refs
content = eregi_replace("\\s\\s+","",content) ' whitespace
strip_tags1 = content
End Function
' ======================================================
'// 허용태그 외의 모든 태그제거
'// 사용법 : strip_tags2(content, allowtags)
'// content = "....."
'// allowtags = "br,a,img,table,b,font,div,center,embed" ' 허용 ※ 태그 붙여서 연속으로, 공백 오류발생
'// strip_tags2(content, allowtags)
'// ======================================================
Function strip_tags2(str,allowtags)
Dim content, tags
content = str
tags = replace(allowtags,",","|")
content = eregi_replace("<(/?)(?!/|" & tags & ")([^<>]*)?>","<$1$2>",content)
content = eregi_replace("(javascript|vbscript)+","$1//",content)
content = eregi_replace("(.location|location.|onload=|.cookie|alert|window.open|onmouse|onkey|onclick|view-source)+","//",content) '// 자바스크립트 실행방지
strip_tags2 = content
End Function
%>
< 테스트 >
<%
Dim allowtags, content
allowtags = "br,a,img,b,font,div,center,embed" ' 허용 ※ 태그 붙여서 연속으로, 공백 오류발생
content = "<font color=red>허용하지 않은 태그</font>가<br />잘 <b>보이나요?</b><br /><script></script>"
content = content & "<table><div align=center>아주 유용할꺼에요~</div><body><html><xmp><pre>"
response.write "strip_tags1 결과<br />"& strip_tags1(content) &"<br /><br />"
response.write "strip_tags2 결과<br />"& strip_tags2(content, allowtags)
%>
< 결과 >
< 소스보기 >
strip_tags1 결과
허용하지 않은 태그가잘 보이나요?아주 유용할꺼에요~
strip_tags2 결과
<font color=red>허용하지 않은 태그</font>가<br />잘 <b>보이나요?</b><br /><script></script><table><div align=center>아주 유용할꺼에요~</div><body><html><xmp><pre>
참고자료
http://flashcafe.org/3717
http://dualist.tistory.com/115
'// ======================================================
'// html 태그 속성 제거
'// 이용방법
'// content = "....."
'// contents = RemoveHTMLAttributes(contents)
'// ======================================================
function RemoveHTMLAttributes(strText)
set rex = new Regexp
rex.Pattern= "<[^>]+>"
rex.Global=true
strText=rex.Replace(strText,"")
strText=Replace(strText,"·","·")
RemoveHTMLAttributes=Replace(strText," "," ")
end function
'// ======================================================
'// HTML 태그 제거
'// 이용방법
'// content = "....."
'// contents = RemoveHTML(contents)
'// http://www.codeproject.com/Articles/639/Removing-HTML-from-the-text-in-ASP
'// ======================================================
Function RemoveHTML(strText)
Dim TAGLIST
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;"
Const BLOCKTAGLIST = ";APPLET;EMBED;FRAMESET;HEAD;NOFRAMES;NOscRIPT;OBJECT;scRIPT;STYLE;"
Dim nPos1, nPos2, nPos3, strResult, strTagName, bRemove, bSearchForBlock
nPos1 = InStr(strText, "<")
Do While nPos1 > 0
nPos2 = InStr(nPos1 + 1, strText, ">")
If nPos2 > 0 Then
strTagName = Mid(strText, nPos1 + 1, nPos2 - nPos1 - 1)
strTagName = Replace(Replace(strTagName, vbCr, " "), vbLf, " ")
nPos3 = InStr(strTagName, " ")
If nPos3 > 0 Then
strTagName = Left(strTagName, nPos3 - 1)
End If
If Left(strTagName, 1) = "/" Then
strTagName = Mid(strTagName, 2)
bSearchForBlock = False
Else
bSearchForBlock = True
End If
If InStr(1, TAGLIST, ";" & strTagName & ";", vbTextCompare) > 0 Then
bRemove = True
If bSearchForBlock Then
If InStr(1, BLOCKTAGLIST, ";" & strTagName & ";", vbTextCompare) > 0 Then
nPos2 = Len(strText)
nPos3 = InStr(nPos1 + 1, strText, "</" & strTagName, vbTextCompare)
If nPos3 > 0 Then
nPos3 = InStr(nPos3 + 1, strText, ">")
End If
If nPos3 > 0 Then
nPos2 = nPos3
End If
End If
End If
Else
bRemove = False
End If
If bRemove Then
strResult = strResult & Left(strText, nPos1 - 1)
strText = Mid(strText, nPos2 + 1)
Else
strResult = strResult & Left(strText, nPos1)
strText = Mid(strText, nPos1 + 1)
End If
Else
strResult = strResult & strText
strText = ""
End If
nPos1 = InStr(strText, "<")
Loop
strResult = strResult & strText
RemoveHTML = strResult
End Function
%>
< 이용 방법 >
<%
contents = RemoveHTML(contents)
%>
<%
'// ======================================================
'// 패턴으로 치환할수 있는 eregi_replace()함수
'// PHP에는 있으나 ASP에는 없기 때문
'// ======================================================
Function eregi_replace(pattern, replace, text)
Dim eregObj
Set eregObj = New RegExp
eregObj.Pattern = pattern '패턴 설정
eregObj.IgnoreCase = True '대소문자 구분 여부
eregObj.Global = True '전체 문서에서 검색
eregi_replace = eregObj.Replace(text, replace) 'Replace String
End Function
'// ======================================================
'// 모든 태그제거
'// 사용법 : strip_tags1(content)
'// content = "....."
'// strip_tags1(content)
'// ======================================================
Function strip_tags1(str)
Dim content
content = str
content = eregi_replace("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>","",content) ' all
content = eregi_replace("<(no)?script[^>]*>.*?</(no)?script>","",content) ' scripts
content = eregi_replace("<style[^>]*>.*</style>", "", content) ' style
'content = eregi_replace("<span[^>]*>.*</span>", "", content) ' span
content = eregi_replace("<(\""[^\""]*\""|\'[^\']*\'|[^\'\"">])*>","",content) ' TAGS
content = eregi_replace("<\\w+\\s+[^<]*\\s*>","",content) ' nTAGS
'content = eregi_replace("&[^;]+;","",content) ' entity_refs
content = eregi_replace("[^;]+;","",content) ' entity_refs
content = eregi_replace("\\s\\s+","",content) ' whitespace
strip_tags1 = content
End Function
' ======================================================
'// 허용태그 외의 모든 태그제거
'// 사용법 : strip_tags2(content, allowtags)
'// content = "....."
'// allowtags = "br,a,img,table,b,font,div,center,embed" ' 허용 ※ 태그 붙여서 연속으로, 공백 오류발생
'// strip_tags2(content, allowtags)
'// ======================================================
Function strip_tags2(str,allowtags)
Dim content, tags
content = str
tags = replace(allowtags,",","|")
content = eregi_replace("<(/?)(?!/|" & tags & ")([^<>]*)?>","<$1$2>",content)
content = eregi_replace("(javascript|vbscript)+","$1//",content)
content = eregi_replace("(.location|location.|onload=|.cookie|alert|window.open|onmouse|onkey|onclick|view-source)+","//",content) '// 자바스크립트 실행방지
strip_tags2 = content
End Function
%>
< 테스트 >
<%
Dim allowtags, content
allowtags = "br,a,img,b,font,div,center,embed" ' 허용 ※ 태그 붙여서 연속으로, 공백 오류발생
content = "<font color=red>허용하지 않은 태그</font>가<br />잘 <b>보이나요?</b><br /><script></script>"
content = content & "<table><div align=center>아주 유용할꺼에요~</div><body><html><xmp><pre>"
response.write "strip_tags1 결과<br />"& strip_tags1(content) &"<br /><br />"
response.write "strip_tags2 결과<br />"& strip_tags2(content, allowtags)
%>
< 결과 >
< 소스보기 >
strip_tags1 결과
허용하지 않은 태그가잘 보이나요?아주 유용할꺼에요~
strip_tags2 결과
<font color=red>허용하지 않은 태그</font>가<br />잘 <b>보이나요?</b><br /><script></script><table><div align=center>아주 유용할꺼에요~</div><body><html><xmp><pre>
참고자료
http://flashcafe.org/3717
http://dualist.tistory.com/115
댓글목록
등록된 댓글이 없습니다.