All the links in the web page http://php.net
were extracted with simplexml_import_dom in code1.
code1
<?php
$dom = new DOMDocument();
$dom->loadHTMLFile('http://php.net');
$xml = simplexml_import_dom($dom);
$nodes = $xml->xpath('//a[@href]');
foreach ($nodes as $node) {
echo $node['href'], "<br />
";
}
?>
Now i want parse the web page with DOMXPath,change simplexml_import_dom in code1 into DOMXPath in code2,there is a bug in code2 ,how to fix it?
code2
<?php
$html = file_get_contents('http://php.net');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a[@href]');
foreach ($nodes as $node) {
echo $node['href'], "<br />
";
}
?>