3초기억력

ASP - 정규식을 이용한 html 태그 제거 함수 본문

플밍_ASP

ASP - 정규식을 이용한 html 태그 제거 함수

잠수콩 2011. 3. 29. 15:05
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


출처: http://snippets.dzone.com

Function stripTags(HTMLstring)
             Set RegularExpressionObject = New RegExp
            With RegularExpressionObject
                      .Pattern = "<[^>]+>"
                      .IgnoreCase = True
                      .Global = True
            End With
            stripTags = RegularExpressionObject.Replace(HTMLstring, "")
            Set RegularExpressionObject = nothing
End Function


-----------------------------------------------------
출처 : http://dualist.tistory.com/115
 

01.<%
02.'//패턴으로 치환할수 있는 eregi_replace()함수를 구현
03.'//PHP에는 있으나 ASP에는 없기 때문
04.Function eregi_replace(pattern, replace, text)
05.Dim eregObj:
06. 
07.Set eregObj = New RegExp:
08. 
09.eregObj.Pattern = pattern: '//패턴 설정
10.eregObj.IgnoreCase = True'//대소문자 구분 여부
11.eregObj.Global = True'//전체 문서에서 검색
12. 
13.eregi_replace = eregObj.Replace(text, replace): '//Replace String
14.End Function
15. 
16.'//허용태그 외의 모든 태그제거 함수
17.Function strip_tags(str)
18.Dim content, pattern:
19. 
20.content = str:
21. 
22.'pattern = "<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>"'All->이건 제대로 동작하지 않는다. 참고용으로 남겨둠
23.'content = eregi_replace(pattern, "", content):
24.pattern = "<(no)?script[^>]*>.*?</(no)?script>":'SCRIPTS
25.content = eregi_replace(pattern, "", content):
26.pattern = "<style[^>]*>.*</style>":'STYLE
27.content = eregi_replace(pattern, "", content):
28.pattern = "<(\""[^\""]*\""|\'[^\']*\'|[^\'\"">])*>":'TAGS
29.content = eregi_replace(pattern, "", content):
30.pattern = "<\\w+\\s+[^<]*\\s*>":'nTAGS
31.content = eregi_replace(pattern, "", content):
32.pattern = "&[^;]+;":'ENTITY_REFS
33.content = eregi_replace(pattern, "", content):
34.pattern = "\\s\\s+":'WHITESPACE
35.content = eregi_replace(pattern, "", content):
36. 
37.strip_tags = content:
38.End Function
39. 
40.'//사용예
41.Dim comment:
42. 
43.comment = "<font color=red>허용하지 않은 태그</font>가<br>잘 <b>보이나요?</b><br><script></script>":
44.comment = comment & "<div align=center>아주 유용할꺼에요~</div><body><html><xmp><pre>":
45. 
46.response.write strip_tags(comment):
47.
%> 
Comments