- print_r: documentation ( source)
- libxml_clear_errors: documentation ( source)
- libxml_use_internal_errors: documentation ( source)
<?php
$html = ' Here is some text and also:
<figure class="class-one">
<img src="/example.jpg"
alt="example alt"
class="some-image-class">
<figcaption>example caption</figcaption>
</figure>
And another one (and many more):
<figure class="class-one some-other-class">
<img src="/example2.jpg"
alt="example2 alt">
</figure>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$images = $dom->getElementsByTagName('img');
$figures = $dom->getElementsByTagName('figure');
$figures = array();
foreach ($figures as $figure) {
$figures['class'] = $figure->getAttribute('class');
}
print_r($figures);