3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 5.3.29, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Array ( )

preferences:
107.86 ms | 407 KiB | 5 Q