3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<HTML <div id="my-cats"> <ul class="category_list"> <li class="item reference"> <span class="the_score"><b>35</b></span> <span class="the_category">Reference / Education</span> </li> <li class="item computer"> <span class="the_score"><b>50</b></span> <span class="the_category">Computer / Internet</span> </li> </ul> <ul class="category_list"> <li class="item home"> <span class="the_score"><b>22</b></span> <span class="the_category">Home / Gardening</span> </li> <li class="item home"> <span class="the_score"><b>12</b></span> <span class="the_category">Home / Repair</span> </li> </ul> </div> HTML; $dom = new DOMDocument(); @$dom->loadHTML($html); $finder = new DOMXPath($dom); $cats = $finder->query('//div[@id="my-cats"]//ul[@class="category_list"]//li'); foreach( $cats as $li ){ echo $li->getAttribute('class') . "\n"; $value = trim($finder->query('//span[@class="the_score"]', $li)->item(0)->nodeValue); $key = trim($finder->query('//span[@class="the_category"]', $li)->item(0)->nodeValue); echo "$key : $value\n"; }
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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.34, 8.2.0 - 8.2.35, 8.3.0 - 8.3.36, 8.4.1 - 8.4.27, 8.5.0 - 8.5.12
item reference Reference / Education : 35 item computer Reference / Education : 35 item home Reference / Education : 35 item home Reference / Education : 35

preferences:
33.04 ms | 1508 KiB | 8 Q