3v4l.org

run code in 500+ PHP versions simultaneously
<?php $dom = new DOMDocument; $file = <<<DATA <body> <div class="test"> <p> <img src="loading.gif" data-src="https://test.com/images/images/120/1313131313232.jpg" alt="test" /> </p> <img src="loading.gif" data-src="https://test.com/images/images/120/1313131313232.jpg" alt="test" /> </div> <div class="test"> This is text <img src="loading.gif" data-src="https://test.com/images/images/120/1313131313232.jpg" alt="test" /> </div> <div> <img src="loading.gif" data-src="https://test.com/images/images/120/1313131313232.jpg" alt="test" /> </div> </body> DATA; @$dom->loadHTML($file); $xpath = new DOMXPath($dom); $divs = $xpath->query('//div[@class="test"]'); foreach ($divs as $key => $div) { echo $div->textContent . PHP_EOL; foreach ($div->getElementsByTagName("img") as $img) { if ($img->getAttribute('alt') === 'test') { echo $img->getAttribute('data-src') . PHP_EOL; } } }
Output for 8.0.30, 8.1.22 - 8.1.34, 8.2.9 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
https://test.com/images/images/120/1313131313232.jpg https://test.com/images/images/120/1313131313232.jpg This is text https://test.com/images/images/120/1313131313232.jpg
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
https://test.com/images/images/120/1313131313232.jpg https://test.com/images/images/120/1313131313232.jpg This is text https://test.com/images/images/120/1313131313232.jpg

preferences:
105.54 ms | 1987 KiB | 4 Q