Hi everyone,

Quick question about DomDocument::loadHTML (http://www.php.net/manual/en/function.dom-domdocument-loadhtml.php)

Why does this not create a parse-tree?

$doc = new DOMDocument();
$doc->loadHTML("<html><body><p>Test</p></body></html>");
var_dump( $doc );

prints but a blank object, and otherwise:

var_dump( $doc->getElementsByTagName( 'p' ) );

returns a blank structure on my machine.

Normal? I was hoping to be able to generate HTML parse-trees from strings.

Thanks.
Alex

    Perhaps you can try this:

    <?php
    $doc = new DOMDocument();
    $doc->loadHTML("<html><body><p>Test</p></body></html>");
    $list = $doc->getElementsByTagName('p');
    for ($i = 0; $i < $list->length; ++$i) {
        echo $list->item($i)->nodeValue . "<br />\n";
    }
    ?>

      thanks for that snippet - so the issue is solely that the elements have no var_dump or print_r representation?

        thanks for that snippet - so the issue is solely that the elements have no var_dump or print_r representation?

        They probably do, just that it is not what you are looking to check, unlike say, for arrays.

          Write a Reply...