Sorry if this is reposted, but I can't wrap my mind around it and I've tried all the available documentation and examples I could find.
I am trying to get the first img
element of a string containing HTML
PHP
$html = '<p><img src="http://placekitten.com/200/300" alt="" width="200" height="300" /></p>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$imgs = $dom->getElementsByTagName('img');
var_dump($imgs);
This spits object(DOMNodeList)#57 (0) { }
when it should find the one occurrence.
I've tried with XPath with no luck either.
Use this:
$img = $dom->getElementsByTagName('img')->item(0);
echo $img->attributes->getNamedItem("src")->value;