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;

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0120.00318.70
8.3.50.0150.00616.83
8.3.40.0160.00019.09
8.3.30.0150.00319.34
8.3.20.0040.00420.60
8.3.10.0060.00320.77
8.3.00.0040.00419.51
8.2.170.0040.01122.96
8.2.160.0090.00620.76
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0030.00726.16
8.2.120.0040.00422.64
8.2.110.0060.00322.63
8.2.100.0040.00818.16
8.2.90.0040.00418.28
8.2.80.0040.00417.97
8.2.70.0030.00617.88
8.2.60.0000.00818.30
8.2.50.0060.00618.07
8.2.40.0040.00418.47
8.2.30.0040.00418.32
8.2.20.0060.00318.04
8.2.10.0070.00018.64
8.2.00.0040.00418.01
8.1.270.0060.00323.91
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0100.00722.85
8.1.230.0040.00823.00
8.1.220.0040.00418.29
8.1.210.0050.00319.22
8.1.200.0040.00717.73
8.1.190.0050.00317.63
8.1.180.0050.00318.10
8.1.170.0040.00419.00
8.1.160.0080.00022.29
8.1.150.0040.00419.31
8.1.140.0030.00617.82
8.1.130.0030.00318.15
8.1.120.0000.00817.90
8.1.110.0000.00717.80
8.1.100.0000.00717.83
8.1.90.0000.00817.74
8.1.80.0030.00517.87
8.1.70.0030.00517.72
8.1.60.0030.00517.97
8.1.50.0060.00317.87
8.1.40.0040.00417.78
8.1.30.0000.00918.06
8.1.20.0050.00317.98
8.1.10.0040.00417.88
8.1.00.0040.00417.76
8.0.300.0030.00620.35
8.0.290.0040.00417.25
8.0.280.0000.00718.90
8.0.270.0030.00517.54
8.0.260.0050.00217.53
8.0.250.0030.00317.27
8.0.240.0040.00417.42
8.0.230.0070.00017.45
8.0.220.0000.00717.36
8.0.210.0040.00417.19
8.0.200.0040.00417.44
8.0.190.0100.00017.26
8.0.180.0000.00717.41
8.0.170.0000.00817.37
8.0.160.0030.00517.32
8.0.150.0030.00517.18
8.0.140.0000.00817.19
8.0.130.0000.00613.88
8.0.120.0050.00317.32
8.0.110.0040.00417.29
8.0.100.0070.00017.30
8.0.90.0040.00417.30
8.0.80.0030.01217.25
8.0.70.0000.00717.21
8.0.60.0040.00417.17
8.0.50.0040.00417.30
8.0.30.0160.00317.71
8.0.20.0100.00917.50
8.0.10.0080.00017.35
8.0.00.0100.00817.21
7.4.330.0000.00515.52
7.4.320.0030.00316.92
7.4.300.0030.00316.95
7.4.290.0070.00016.84
7.4.280.0040.00416.96
7.4.270.0040.00416.98
7.4.260.0000.00716.83
7.4.250.0040.00416.85
7.4.240.0020.00516.89
7.4.230.0000.00817.01
7.4.220.0000.00716.85
7.4.210.0130.00716.76
7.4.200.0070.00016.80
7.4.160.0140.01016.83
7.4.150.0130.01317.40
7.4.140.0120.00617.86
7.4.130.0080.01016.93
7.4.120.0050.01416.89
7.4.110.0080.01016.84
7.4.100.0030.01816.70
7.4.90.0090.00916.70
7.4.80.0060.01219.39
7.4.70.0060.01316.87
7.4.60.0000.01817.01
7.4.50.0040.00417.02
7.4.40.0040.01617.07
7.4.30.0110.00616.89
7.3.330.0000.00613.53
7.3.320.0000.00513.59
7.3.310.0030.00316.70
7.3.300.0050.00316.63
7.3.290.0060.01016.71
7.3.280.0080.01016.69
7.3.270.0120.00617.40
7.3.260.0130.00616.92
7.3.250.0120.00616.82
7.3.240.0140.00317.05
7.3.230.0110.00616.91
7.3.210.0040.01716.95
7.3.200.0070.01719.39
7.3.190.0070.01516.96
7.3.180.0120.00616.60
7.3.170.0030.01716.71
7.3.160.0110.00616.73
7.2.330.0090.00916.57
7.2.320.0160.00616.18
7.2.310.0160.00316.37
7.2.300.0060.00916.13
7.2.290.0160.00616.47
5.4.320.0060.03612.47
5.4.310.0050.03912.47
5.4.300.0090.03912.47
5.4.290.0070.04012.47
5.4.280.0070.03612.36
5.4.270.0090.03512.37
5.4.260.0060.04212.36
5.4.250.0060.03712.37
5.4.240.0050.03612.37
5.4.230.0040.03912.36
5.4.220.0060.03612.36
5.4.210.0060.03712.36
5.4.200.0080.03412.36
5.4.190.0050.03812.36
5.4.180.0060.03612.35
5.4.170.0070.03812.37
5.4.160.0070.03712.36
5.4.150.0060.04212.36
5.4.140.0090.04212.04
5.4.130.0080.03512.03
5.4.120.0040.03711.99
5.4.110.0070.03911.99
5.4.100.0090.03811.99
5.4.90.0050.03711.99
5.4.80.0040.04211.99
5.4.70.0090.04311.98
5.4.60.0120.04211.98
5.4.50.0050.03711.98
5.4.40.0070.04511.97
5.4.30.0060.04311.96
5.4.20.0050.03811.96
5.4.10.0070.03511.96
5.4.00.0060.03811.46
5.3.290.0100.04112.80
5.3.280.0050.03912.71
5.3.270.0050.03912.72
5.3.260.0070.03812.72
5.3.250.0060.03812.72
5.3.240.0060.03712.72
5.3.230.0080.04212.71
5.3.220.0070.03512.68
5.3.210.0070.03912.68
5.3.200.0070.03812.68
5.3.190.0080.04312.68
5.3.180.0070.04312.67
5.3.170.0080.03812.67
5.3.160.0040.04212.67
5.3.150.0060.03812.67
5.3.140.0070.03512.66
5.3.130.0050.03812.66
5.3.120.0070.04112.66
5.3.110.0050.03912.66
5.3.100.0040.03812.12
5.3.90.0050.03812.08
5.3.80.0070.03512.07
5.3.70.0050.03912.08
5.3.60.0060.03612.06
5.3.50.0070.03612.00
5.3.40.0050.04312.00
5.3.30.0080.03911.96
5.3.20.0070.04111.73
5.3.10.0070.03411.70
5.3.00.0040.03911.69
5.2.170.0050.0309.19
5.2.160.0070.0279.19
5.2.150.0050.0349.19
5.2.140.0080.0279.19
5.2.130.0050.0329.15
5.2.120.0060.0299.15
5.2.110.0050.0309.15
5.2.100.0040.0299.15
5.2.90.0060.0289.15
5.2.80.0070.0319.14
5.2.70.0040.0329.14
5.2.60.0040.0349.10
5.2.50.0050.0299.07
5.2.40.0050.0289.04
5.2.30.0040.0319.02
5.2.20.0050.0279.03
5.2.10.0070.0298.95
5.2.00.0040.0328.81
5.1.60.0020.0278.10
5.1.50.0060.0238.10
5.1.40.0040.0308.08
5.1.30.0050.0258.42
5.1.20.0070.0278.45
5.1.10.0070.0248.18
5.1.00.0050.0258.18
5.0.50.0030.0216.66
5.0.40.0040.0216.52
5.0.30.0030.0346.33
5.0.20.0040.0196.30
5.0.10.0040.0216.27
5.0.00.0040.0326.27
4.4.90.0040.0154.78
4.4.80.0020.0174.75
4.4.70.0020.0164.76
4.4.60.0020.0164.76
4.4.50.0030.0154.77
4.4.40.0020.0274.71
4.4.30.0040.0144.76
4.4.20.0040.0144.84
4.4.10.0040.0144.85
4.4.00.0020.0274.76
4.3.110.0020.0164.67
4.3.100.0020.0164.67
4.3.90.0030.0144.63
4.3.80.0040.0244.58
4.3.70.0030.0154.63
4.3.60.0020.0154.63
4.3.50.0020.0164.63
4.3.40.0030.0234.54
4.3.30.0020.0173.31
4.3.20.0030.0153.29
4.3.10.0030.0143.24
4.3.00.0070.01716.59

preferences:
48.55 ms | 400 KiB | 5 Q