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
Post a Comment