code_junkie

Checking for and not printing javascript in generated data?

Nov 11th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #from http://www.perlmonks.org/?node_id=161281
  2. sub untag {
  3. local $_ = $_[0] || $_;
  4. # ALGORITHM:
  5. # find < ,
  6. # comment <!-- ... -->,
  7. # or comment <? ... ?> ,
  8. # or one of the start tags which require correspond
  9. # end tag plus all to end tag
  10. # or if s or ="
  11. # then skip to next "
  12. # else [^>]
  13. # >
  14. s{
  15. < # open tag
  16. (?: # open group (A)
  17. (!--) | # comment (1) or
  18. (?) | # another comment (2) or
  19. (?i: # open group (B) for /i
  20. ( TITLE | # one of start tags
  21. SCRIPT | # for which
  22. APPLET | # must be skipped
  23. OBJECT | # all content
  24. STYLE # to correspond
  25. ) # end tag (3)
  26. ) | # close group (B), or
  27. ([!/A-Za-z]) # one of these chars, remember in (4)
  28. ) # close group (A)
  29. (?(4) # if previous case is (4)
  30. (?: # open group (C)
  31. (?! # and next is not : (D)
  32. [s=] # s or "="
  33. ["`'] # with open quotes
  34. ) # close (D)
  35. [^>] | # and not close tag or
  36. [s=] # s or "=" with
  37. `[^`]*` | # something in quotes ` or
  38. [s=] # s or "=" with
  39. '[^']*' | # something in quotes ' or
  40. [s=] # s or "=" with
  41. "[^"]*" # something in quotes "
  42. )* # repeat (C) 0 or more times
  43. | # else (if previous case is not (4))
  44. .*? # minimum of any chars
  45. ) # end if previous char is (4)
  46. (?(1) # if comment (1)
  47. (?<=--) # wait for "--"
  48. ) # end if comment (1)
  49. (?(2) # if another comment (2)
  50. (?<=?) # wait for "?"
  51. ) # end if another comment (2)
  52. (?(3) # if one of tags-containers (3)
  53. </ # wait for end
  54. (?i:3) # of this tag
  55. (?:s[^>]*)? # skip junk to ">"
  56. ) # end if (3)
  57. > # tag closed
  58. }{}gsx; # STRIP THIS TAG
  59. return $_ ? $_ : "";
  60. }
  61.  
  62. $output = strip_tags($input);
  63.  
  64. $output = strip_tags($input, '<code><em><strong>');
Add Comment
Please, Sign In to add comment