fork(5) download
  1. <?php
  2.  
  3. $redefs = '(?(DEFINE)
  4. (?<tagname> [a-z][^\s>/]*+ )
  5. (?<attname> [^\s>/][^\s=>/]*+ ) # first char can be pretty much anything, including =
  6. (?<attval> (?>
  7. "[^"]*+" |
  8. \'[^\']*+\' |
  9. [^\s>]*+ # unquoted values can contain quotes, = and /
  10. )
  11. )
  12. (?<attrib> (?&attname)
  13. (?: \s*+
  14. = \s*+
  15. (?&attval)
  16. )?+
  17. )
  18. (?<crap> [^\s>] ) # most crap inside tag is ignored, will eat the last / in self closing tags
  19. (?<tag> <(?&tagname)
  20. (?: \s*+ # spaces between attributes not required: <b/foo=">"style=color:red>bold red text</b>
  21. (?>
  22. (?&attrib) | # order matters
  23. (?&crap) # if not an attribute, eat the crap
  24. )
  25. )*+
  26. \s*+ /?+
  27. \s*+ >
  28. )
  29. )';
  30.  
  31.  
  32. // removes onanything attributes from all matched HTML tags
  33. function remove_event_attributes($html){
  34. global $redefs;
  35. $re = '(?&tag)' . $redefs;
  36. return preg_replace("~$re~xie", 'remove_event_attributes_from_tag("$0")', $html);
  37. }
  38.  
  39. // removes onanything attributes from a single opening tag
  40. function remove_event_attributes_from_tag($tag){
  41. global $redefs;
  42. $re = '( ^ <(?&tagname) ) | \G \s*+ (?> ((?&attrib)) | ((?&crap)) )' . $redefs;
  43. return preg_replace("~$re~xie", '"$1$3"? "$0": (preg_match("/^on/i", "$2")? " ": "$0")', $tag);
  44. }
  45.  
  46.  
  47. // test
  48. $str = '
  49. <button onclick="..javascript instruction..">
  50. <button onclick="..javascript instruction.." value="..">
  51. <button onclick=..javascript_instruction..>
  52. <button onclick=..javascript_instruction.. value>
  53. <hello word "" ontest = "hai"x="y"onfoo=bar/baz />
  54. ';
  55.  
  56. echo $str . "\n----------------------\n";
  57.  
  58. echo remove_event_attributes($str);
  59.  
  60. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
<button onclick="..javascript instruction..">
<button onclick="..javascript instruction.." value="..">
<button onclick=..javascript_instruction..>
<button onclick=..javascript_instruction.. value>
<hello word "" ontest = "hai"x="y"onfoo=bar/baz  />

----------------------

<button >
<button  value="..">
<button >
<button  value>
<hello word "" x="y"   />