foreach loop with DOMDocument on PHP -


this code doesn't out thing, 0.

<?php      $html = '<a href="abc" >hello world!</a><a href="abcdef" >hello  </a>';      $html = '<div>' . $html . '</div>';      $doc = new domdocument;      $doc->loadhtml($html);      $links = $doc->getelementsbytagname('a')->item(0);      foreach ($links $link){          echo "0";          echo $dom->savehtml($link->getattribute('href');      }      // outputs: "<h1>hello world!</h1>"  ?> 

you have drop ->item(0) following getelementsbytagname call or first anchor element in $links (which why foreach not want to). savehtml call have removed.

$html = '<a href="abc" >hello world!</a><a href="abcdef" >hello  </a>'; $html = '<div>' . $html . '</div>'; $doc = new domdocument; $doc->loadhtml($html);  // drop ->item(0) $links = $doc->getelementsbytagname('a');  foreach ($links $link){     // remove savehtml call     echo $link->getattribute('href'), php_eol; } 

output:

abc abcdef 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -