Assuming $doc
is a DOMDocument
representing your HTML document:
$output = array();
$xpath = new DOMXPath($doc);
# find each img inside a link
foreach ($xpath->query('//a[@href]//img') as $img) {
# find the link by going up til an <a> is found
# since we only found <img>s inside an <a>, this should always succeed
for ($link = $img; $link->tagName !== 'a'; $link = $link->parentNode);
$output[] = array(
'href' => $link->getAttribute('href'),
'src' => $img->getAttribute('src'),
'alt' => $img->getAttribute('alt'),
);
}