모든 태그 제거 & 허용태그 외의 모든 html 태그 제거 > 기술자료 | 해피정닷컴

모든 태그 제거 & 허용태그 외의 모든 html 태그 제거 > 기술자료

본문 바로가기

사이트 내 전체검색

모든 태그 제거 & 허용태그 외의 모든 html 태그 제거 > 기술자료

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,"&middot;","·")
    RemoveHTMLAttributes=Replace(strText,"&nbsp;"," ")
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 & ")([^<>]*)?>","&lt;$1$2&gt",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 />&lt;script&gt&lt;/script&gt&lt;table&gt<div align=center>아주 유용할꺼에요~</div><body>&lt;html&gt&lt;xmp&gt&lt;pre&gt


참고자료
http://flashcafe.org/3717
http://dualist.tistory.com/115

댓글목록

등록된 댓글이 없습니다.


Total 2,643건 1 페이지
  • RSS
기술자료 목록
2643
PHP   505  2025-02-07 09:27 ~ 2025-02-07 16:59  
2642
그누보드   418  2025-02-07 08:55 ~ 2025-02-07 17:04  
2641
그누보드   672  2024-11-26 21:14 ~ 2024-11-26 21:22  
2640
그누보드   766  2024-11-22 10:52 ~ 2024-11-22 11:03  
2639
호스팅   769  2024-11-19 14:41 ~ 2024-11-19 21:17  
2638
Linux   588  2024-11-18 15:45 ~ 2024-11-18 15:48  
2637
일반   546  2024-11-15 16:45 ~ 2024-11-15 16:46  
2636
Secure   577  2024-11-06 18:48 ~ 2024-11-06 18:50  
2635
영카트   798  2024-10-21 13:44 ~ 2024-10-21 19:42  
2634
전자결제   1636  2024-09-05 09:30  
2633
MySQL   1448  2024-03-29 14:14 ~ 2024-03-29 14:14  
2632
그누보드   1670  2024-02-23 18:40 ~ 2024-02-24 06:13  
2631
JavaScript   1784  2024-02-16 18:50 ~ 2024-02-16 20:37  
2630
Java   1767  2024-02-06 16:49  
2629
PHP   1960  2024-02-06 16:42  
2628
호스팅   1740  2024-01-29 12:54  
2627
PHP   1634  2024-01-26 11:04 ~ 2024-01-26 11:13  
2626
MySQL   1757  2024-01-08 17:37 ~ 2024-03-14 16:00  
2625
SQL   2098  2024-01-08 12:36  
2624
영카트   2103  2024-01-04 14:57  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2025 해피정닷컴. All Rights Reserved.