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) { }
Some
DOMDocumentdebugging hints.I take your small example and will add some hints how to debug the code:
Did the loading work? That is this line:
You can take a look inside the document by outputting it’s content. If you display that in the browser you need to look into the source of your document or you just change the output with
htmlspecialchars:This will give you the documented as loaded in the HTML variant verbatim inside your browser.
The next part you might want to debug is the result of
getElementsByTagName:First assign it to a variable, and then check the
lengthif it’s notNULLorFALSE:The length will tell you how many elements were matched.
Example/Demo:
Output:
Hope this is helpful.