abiku
abiku

Reputation: 2246

DOMDocument not working

I want to find all images in the html string like in the code below. Why all results are null in this example (except node name which gives 'img')? I want to display the image html string for each image.

 $html = '<img src=\'dasdasdasd.jpg\' >';

$dom = new DOMDocument();
$dom->loadHTML($html);
$els = $dom->getElementsByTagName('img');

foreach ( $els as $el ) {
    $nodeName = strtolower($el->nodeName);
    $nodetext =  strtolower($el->textContent);
    $nodeval = strtolower($el->nodeValue);
        var_dump($nodeName);
        var_dump($nodetext);
        var_dump($nodeval);
}

Upvotes: 0

Views: 2241

Answers (1)

blue112
blue112

Reputation: 56572

Well, that node doesn't have any value or text...

What you want is the attribute:

$src = $el->getAttribute("src");
var_dump($src); //dasdasdasd.jpg

As commented, if you need the whole XML, you can use

$xml = $dom->savexml($el);
echo $xml;

Upvotes: 1

Related Questions