fork(3) download
  1. <?php
  2. $html_string = '<div id="myelementID" class="hello" data-foo="bar">...</div>';
  3.  
  4. $dom = new DomDocument;
  5. $dom -> loadHTML($html_string);
  6. $el = $dom -> getElementById("myelementID");
  7.  
  8. if (!empty($el))
  9. {
  10. $elemString = "<div";
  11. foreach ($el -> attributes as $attr)
  12. {
  13. $name = $attr -> nodeName;
  14. $value = $attr -> nodeValue;
  15. $elemString .= " {$name}=\"{$value}\"";
  16. }
  17. $elemString .= ">";
  18. echo $elemString;
  19. }
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
<div id="myelementID" class="hello" data-foo="bar">