3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @link http://stackoverflow.com/questions/26293581/how-to-select-2nd-element-with-same-tag-using-dom-xpath/26324381#26324381 */ $buffer = <<<BUFFER <div class="fly"> <img src="a.png" class="badge"> <img class="aye" data-original="b.png" width="130" height="253" /> <div class="to"> <h4>Fly To The Moon</h4> <div class="clearfix"> <div class="the"> <h4>**Wow**</h4> </div> <div class="moon"> <h4>**Great**</h4> </div> </div> </div> </div> BUFFER; $doc = new DOMDocument(); $doc->loadHTML($buffer); $xpath = new DOMXPath($doc); /*** * example with iteration ***/ // all <div> tags with class="fly" $divs = $xpath->evaluate("//div[@class='fly']"); foreach ($divs as $div) { // the first data-original attribute of an <img> inside $div echo $xpath->evaluate("string(./img/@data-original)", $div), "<br/>\n"; // all <h4> tags anywhere inside the $div $h4s = $xpath->evaluate('.//h4[not(following-sibling::*//h4)]', $div); foreach ($h4s as $h4) { echo $h4->nodeValue, "<br/>\n"; } } echo "\n----------\n\n"; /*** * example with string evaluation ***/ echo <<<HTML {$xpath->evaluate("string(//div[@class='fly']/img/@data-original)")}<br/> {$xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][1])")}<br/> {$xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][2])")}<br/> <hr/> HTML;
Output for git.master, git.master_jit, rfc.property-hooks
b.png<br/> **Wow**<br/> **Great**<br/> ---------- b.png<br/> **Wow**<br/> <br/> <hr/>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
47.73 ms | 401 KiB | 8 Q