Skip to content

Instantly share code, notes, and snippets.

@lonalore
Created August 16, 2019 13:55
Show Gist options
  • Save lonalore/5a806e3e67a7cbf31690107d1f034192 to your computer and use it in GitHub Desktop.
Save lonalore/5a806e3e67a7cbf31690107d1f034192 to your computer and use it in GitHub Desktop.
PHP: get image src attribute (dom)
<?php
$doc = new DOMDocument();
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
$images = $xml->xpath('//img');
foreach ($images as $img) {
echo " Image Source : " . $img['src'] . "";
echo " Image Alt : " . $img['alt'] . "";
echo " Image Title : " . $img['title'] . "";
echo " Image Width : " . $img['data-src-width']. "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment