모든 태그 제거 & 허용태그 외의 모든 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,633건 5 페이지
  • RSS
기술자료 목록
2553
Android   2945  2022-11-09 15:35 ~ 2022-11-23 14:46  
2552
Android   3116  2022-11-08 19:18 ~ 2022-11-09 13:28  
2551
MySQL   2695  2022-10-31 16:21 ~ 2022-11-01 09:51  
2550
PHP   3765  2022-10-13 20:59 ~ 2022-10-14 09:44  
2549
MySQL   2907  2022-10-12 17:39  
2548
SQL   2878  2022-10-12 17:24  
2547
APP   3401  2022-10-11 14:23 ~ 2022-11-11 15:22  
2546
그누보드   3381  2022-10-07 20:11  
2545
PHP   3339  2022-10-06 14:29  
2544
PHP   2780  2022-10-06 12:20 ~ 2022-10-06 12:29  
2543
MySQL   4205  2022-09-29 00:24 ~ 2022-09-29 00:25  
2542
SQL   3635  2022-09-27 18:26  
2541
SQL   4247  2022-09-26 11:12  
2540
APP   5177  2022-09-21 16:09 ~ 2022-09-21 16:22  
2539
APP   4876  2022-09-21 16:03 ~ 2022-09-21 16:17  
2538
APP   4552  2022-09-15 13:28 ~ 2022-11-08 19:27  
2537
Search   4251  2022-09-03 15:15  
2536
MySQL   6131  2022-07-29 19:40 ~ 2022-07-29 19:49  
2535
JavaScript   7394  2022-07-28 16:08 ~ 2022-07-28 16:13  
2534
전자결제   3942  2022-07-11 15:56 ~ 2022-07-11 15:56  

검색

해피정닷컴 정보

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

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: 서울센터 (08393) 서울시 구로구 디지털로32가길 16 파트너스타워2차 1206-280호
        대전센터 (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 지원1동 205호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 2014-서울구로-0074 [사업자등록확인]  
Copyright 2001~2024 해피정닷컴. All Rights Reserved.