0

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);
}
abiku
  • 2,236
  • 3
  • 25
  • 30
  • possible duplicate of [scraping all images from a website using DOMDocument](http://stackoverflow.com/questions/15895773/scraping-all-images-from-a-website-using-domdocument) – cb0 Jul 21 '15 at 08:01
  • Look here http://stackoverflow.com/questions/15895773/scraping-all-images-from-a-website-using-domdocument – cb0 Jul 21 '15 at 08:02
  • there are different issues in these questions – abiku Jul 21 '15 at 08:06

1 Answers1

1

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;
blue112
  • 52,634
  • 3
  • 45
  • 54