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;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 36
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 36
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 34
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 34
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/Se26V
function name:  (null)
number of ops:  55
compiled vars:  !0 = $buffer, !1 = $doc, !2 = $xpath, !3 = $divs, !4 = $div, !5 = $h4s, !6 = $h4
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   ASSIGN                                                   !0, '%3Cdiv+class%3D%22fly%22%3E%0A++++%3Cimg+src%3D%22a.png%22+class%3D%22badge%22%3E%0A++++%3Cimg+class%3D%22aye%22+data-original%3D%22b.png%22+width%3D%22130%22+height%3D%22253%22+%2F%3E%0A+++++++++%3Cdiv+class%3D%22to%22%3E%0A++++++++++++++%3Ch4%3EFly+To+The+Moon%3C%2Fh4%3E%0A++++++++++++++++++%3Cdiv+class%3D%22clearfix%22%3E%0A+++++++++++++++++++++++%3Cdiv+class%3D%22the%22%3E%0A++++++++++++++++++++++++++++%3Ch4%3E%2A%2AWow%2A%2A%3C%2Fh4%3E%0A+++++++++++++++++++++++%3C%2Fdiv%3E%0A+++++++++++++++++++++++%3Cdiv+class%3D%22moon%22%3E%0A++++++++++++++++++++++++++++%3Ch4%3E%2A%2AGreat%2A%2A%3C%2Fh4%3E%0A+++++++++++++++++++++++%3C%2Fdiv%3E%0A+++++++++++++++++++%3C%2Fdiv%3E%0A++++++++++%3C%2Fdiv%3E%0A%3C%2Fdiv%3E'
   25     1        NEW                                              $8      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $8
   26     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   27     7        NEW                                              $12     'DOMXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $12
   34    11        INIT_METHOD_CALL                                         !2, 'evaluate'
         12        SEND_VAL_EX                                              '%2F%2Fdiv%5B%40class%3D%27fly%27%5D'
         13        DO_FCALL                                      0  $15     
         14        ASSIGN                                                   !3, $15
   36    15      > FE_RESET_R                                       $17     !3, ->36
         16    > > FE_FETCH_R                                               $17, !4, ->36
   39    17    >   INIT_METHOD_CALL                                         !2, 'evaluate'
         18        SEND_VAL_EX                                              'string%28.%2Fimg%2F%40data-original%29'
         19        SEND_VAR_EX                                              !4
         20        DO_FCALL                                      0  $18     
         21        ECHO                                                     $18
         22        ECHO                                                     '%3Cbr%2F%3E%0A'
   42    23        INIT_METHOD_CALL                                         !2, 'evaluate'
         24        SEND_VAL_EX                                              '.%2F%2Fh4%5Bnot%28following-sibling%3A%3A%2A%2F%2Fh4%29%5D'
         25        SEND_VAR_EX                                              !4
         26        DO_FCALL                                      0  $19     
         27        ASSIGN                                                   !5, $19
   44    28      > FE_RESET_R                                       $21     !5, ->34
         29    > > FE_FETCH_R                                               $21, !6, ->34
   45    30    >   FETCH_OBJ_R                                      ~22     !6, 'nodeValue'
         31        ECHO                                                     ~22
         32        ECHO                                                     '%3Cbr%2F%3E%0A'
   44    33      > JMP                                                      ->29
         34    >   FE_FREE                                                  $21
   36    35      > JMP                                                      ->16
         36    >   FE_FREE                                                  $17
   49    37        ECHO                                                     '%0A----------%0A%0A'
   55    38        INIT_METHOD_CALL                                         !2, 'evaluate'
         39        SEND_VAL_EX                                              'string%28%2F%2Fdiv%5B%40class%3D%27fly%27%5D%2Fimg%2F%40data-original%29'
         40        DO_FCALL                                      0  $23     
         41        ROPE_INIT                                     6  ~27     $23
         42        ROPE_ADD                                      1  ~27     ~27, '%3Cbr%2F%3E%0A++++'
   56    43        INIT_METHOD_CALL                                         !2, 'evaluate'
         44        SEND_VAL_EX                                              'string%28%2F%2Fdiv%5B%40class%3D%27fly%27%5D%2F%2Fh4%5Bnot%28following-sibling%3A%3A%2A%2F%2Fh4%29%5D%5B1%5D%29'
         45        DO_FCALL                                      0  $24     
         46        ROPE_ADD                                      2  ~27     ~27, $24
         47        ROPE_ADD                                      3  ~27     ~27, '%3Cbr%2F%3E%0A++++'
   57    48        INIT_METHOD_CALL                                         !2, 'evaluate'
         49        SEND_VAL_EX                                              'string%28%2F%2Fdiv%5B%40class%3D%27fly%27%5D%2F%2Fh4%5Bnot%28following-sibling%3A%3A%2A%2F%2Fh4%29%5D%5B2%5D%29'
         50        DO_FCALL                                      0  $25     
         51        ROPE_ADD                                      4  ~27     ~27, $25
         52        ROPE_END                                      5  ~26     ~27, '%3Cbr%2F%3E%0A%3Chr%2F%3E'
         53        ECHO                                                     ~26
   59    54      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.09 ms | 1399 KiB | 13 Q