I tried using the following code for a HTML page, but it doesn't work. How do I retrieve and manipulate all outputted HTML elements in one page?
$doc = new DOMDocument;
$doc->load('http://localhost/foo/index.php');
$items = $doc->getElementsByTagName('img');
foreach ($items as $item) {
echo $item->nodeValue . "\n";
}
EDIT:
$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
echo $dom->saveHtml($node), PHP_EOL;
}
The code above outputs nothing
Debugging Code:
<?php
$dom = new DOMDocument;
$html = 'http://localhost/foo/index.php';
var_dump($dom->loadHTML($html));
echo '<br />';
var_dump($dom);
echo '<br />';
var_dump($dom->saveHTML());
echo '<br />';
var_dump($dom->getElementsByTagName('a'));
echo '<br />';
foreach ($dom->getElementsByTagName('a') as $node) {
var_dump($node);
echo '<br />';
var_dump( $dom->saveHtml($node) );
echo '<br />';
}
?>
Debugging Result:
bool(true)
object(DOMDocument)#1 (0) { }
string(170) "
http://localhost/foo/index.php
"
object(DOMNodeList)#2 (0) { }
http://localhost/foo/index.php
` - no `A` element at all. – hakre Jun 16 '12 at 12:22