<?php
$html = <<<EOF
<html>
<img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
<figcaption class="caption-text">Caption title here</figcaption>
<img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
<img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
<figcaption class="caption-text">Caption title here</figcaption>
</html>
EOF;
$dom = new DOMdocument();
#libxml_use_internal_errors(true);
$dom->loadHTML($html);
#libxml_clear_errors();
$xpath = new DOMXPath($dom);
$imgs = $xpath->query("//img");
foreach ($imgs as $img) {
if ($img->nextSibling->tagName == 'figcaption') {
$figure = $dom->createElement('figure');
$figure->appendChild($img->cloneNode(true));
$figure->appendChild($img->nextSibling->cloneNode(true));
$img->parentNode->insertBefore($figure, $img);
$img->nextSibling->parentNode->removeChild($img->nextSibling);
$img->parentNode->removeChild($img);
}
}
$dom->formatOutput=true;
echo $dom->saveHTML();