fork(2) download
  1. <?php
  2. $str = <<<EOF
  3. <listing name="name goes there" phone="321321" phone="any phone" attr1="value 1" attr2="value 2">Text description</listing>
  4. <anytag name="another name" phone="any phone" attr1="value 1" attr2="value 2">any can be written&nbsp; where &copy;MyRight</anytag>
  5. <anytag name="another name line 2" phone="65851566" attr1="value &euml;" attr2="value 2">any can be written&nbsp; where &reg;MyRight&euml;ous</anytag>
  6. EOF;
  7. $dom = new DOMDocument();
  8. $dom->loadHTML($str);
  9.  
  10. $nodeList = $dom->getElementsByTagName('anytag');
  11. for($i=0; $i < $nodeList->length; $i++) {
  12. $node = $nodeList->item($i);
  13. if ($node->hasAttributes())
  14. echo $node->nodeName . " =>\n";
  15. foreach ($node->attributes as $attr) {
  16. $name = $attr->nodeName;
  17. $value = $attr->nodeValue;
  18. echo "Attribute '$name'='$value'\n";
  19. }
  20. }
  21. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
anytag =>
Attribute 'name'='another name'
Attribute 'phone'='any phone'
Attribute 'attr1'='value 1'
Attribute 'attr2'='value 2'
anytag =>
Attribute 'name'='another name line 2'
Attribute 'phone'='65851566'
Attribute 'attr1'='value ë'
Attribute 'attr2'='value 2'