fork(1) download
  1. <?php
  2.  
  3. $html = <<<EOF
  4. <html>
  5. <img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
  6. <figcaption class="caption-text">Caption title here</figcaption>
  7.  
  8. <img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
  9.  
  10. <img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354" />
  11. <figcaption class="caption-text">Caption title here</figcaption>
  12. </html>
  13. EOF;
  14.  
  15. $dom = new DOMdocument();
  16. #libxml_use_internal_errors(true);
  17. $dom->loadHTML($html);
  18. #libxml_clear_errors();
  19. $xpath = new DOMXPath($dom);
  20.  
  21. $imgs = $xpath->query("//img");
  22.  
  23. foreach ($imgs as $img) {
  24. if ($img->nextSibling->tagName == 'figcaption') {
  25.  
  26. $figure = $dom->createElement('figure');
  27. $figure->appendChild($img->cloneNode(true));
  28. $figure->appendChild($img->nextSibling->cloneNode(true));
  29.  
  30. $img->parentNode->insertBefore($figure, $img);
  31.  
  32. $img->nextSibling->parentNode->removeChild($img->nextSibling);
  33. $img->parentNode->removeChild($img);
  34.  
  35. }
  36. }
  37. $dom->formatOutput=true;
  38. echo $dom->saveHTML();
Success #stdin #stdout #stderr 0.02s 52472KB
stdin
Standard input is empty
stdout
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://w...content-available-to-author-only...3.org/TR/REC-html40/loose.dtd">
<html><body>
<figure><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></figure><img class="size-full" src="http://w...content-available-to-author-only...e.com/pic.png" alt="name" width="1699" height="354"><figure><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></figure>
</body></html>
stderr
PHP Warning:  DOMDocument::loadHTML(): Tag figcaption invalid in Entity, line: 3 in /home/oYOHfB/prog.php on line 17
PHP Warning:  DOMDocument::loadHTML(): Tag figcaption invalid in Entity, line: 8 in /home/oYOHfB/prog.php on line 17