fork download
  1. <?php
  2.  
  3. $doc = new DOMDocument();
  4.  
  5. $str[] = '<style type="text/css"></style>';
  6. $str[] = '<style type=text/css></style>';
  7. $str[] = '<style TYPE="TEXT/CSS"></style>';
  8. $str[] = '<style TYPE=TEXT/CSS></style>';
  9.  
  10. foreach ($str as $myHtml) {
  11.  
  12. echo "before ", $myHtml, PHP_EOL;
  13.  
  14. $doc->loadHTML($myHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  15.  
  16. removeAttr("style", "type", $doc);
  17.  
  18. echo "after: ", $doc->saveHtml(), PHP_EOL;
  19.  
  20. }
  21.  
  22. function removeAttr($tag, $attr, $doc) {
  23. $nodeList = $doc->getElementsByTagName($tag);
  24. for ($nodeIdx = $nodeList->length; --$nodeIdx >= 0; ) {
  25. $node = $nodeList->item($nodeIdx);
  26. $node->removeAttribute($attr);
  27. }
  28. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
before <style type="text/css"></style>
after: <style></style>

before <style type=text/css></style>
after: <style></style>

before <style TYPE="TEXT/CSS"></style>
after: <style></style>

before <style TYPE=TEXT/CSS></style>
after: <style></style>