3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultApi = '<fieldset class="relative"> <div> <h5><button>CABIN #1</button></h5> </div> <div>EMPTY</div> </fieldset> <fieldset class="relative"> <div> <h5><button>CABIN #2</button></h5> </div> <div> <select data-id="222"> <option value="1">Op#1</option> <option value="2">Op#2</option> </select> </div> </fieldset> <fieldset class="relative"> <div> <h5><button>CABIN #3</button></h5> </div> <div> <select data-id="223"> <option value="1">Op#1</option> <option value="2">Op#2</option> <option value="3">Op#3</option> </select> </div> </fieldset>'; $dom = new \DOMDocument(); @$dom->loadHTML($resultApi); // Create a DOMXPath instance and locate elements $xpath = new \DOMXPath($dom); $items = $xpath->query("//fieldset[@class='relative']"); $result = []; foreach ($items as $node) { $title = $xpath->query('.//following-sibling::h5/button', $node)->item(0)->nodeValue; $div = $xpath->query('.//following-sibling::select', $node); $result[$title]= 0; if($div->length){ foreach ($div as $option) { $children = $option->childNodes; $countOp = 1; foreach ($children as $child) { if($child->nodeName === 'option' && $child->nodeValue !== '-'){ $result[$title]= $countOp++; } } } } } print_r($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 60
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 60
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 59
Branch analysis from position: 38
2 jumps found. (Code = 77) Position 1 = 39, Position 2 = 58
Branch analysis from position: 39
2 jumps found. (Code = 78) Position 1 = 40, Position 2 = 58
Branch analysis from position: 40
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 56
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 56
Branch analysis from position: 45
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 51
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 55
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 55
Branch analysis from position: 51
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 56
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 58
Branch analysis from position: 59
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/XdLFN
function name:  (null)
number of ops:  65
compiled vars:  !0 = $resultApi, !1 = $dom, !2 = $xpath, !3 = $items, !4 = $result, !5 = $node, !6 = $title, !7 = $div, !8 = $option, !9 = $children, !10 = $countOp, !11 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cfieldset+class%3D%22relative%22%3E%0A%09%3Cdiv%3E%0A%09%09%3Ch5%3E%3Cbutton%3ECABIN+%231%3C%2Fbutton%3E%3C%2Fh5%3E%0A%09%3C%2Fdiv%3E%0A%09%3Cdiv%3EEMPTY%3C%2Fdiv%3E%0A%3C%2Ffieldset%3E%0A%3Cfieldset+class%3D%22relative%22%3E%0A%09%3Cdiv%3E%0A%09%09%3Ch5%3E%3Cbutton%3ECABIN+%232%3C%2Fbutton%3E%3C%2Fh5%3E%0A%09%3C%2Fdiv%3E%0A%09%3Cdiv%3E%0A%09%09%3Cselect+data-id%3D%22222%22%3E%0A%09%09%09%3Coption+value%3D%221%22%3EOp%231%3C%2Foption%3E%0A%09%09%09%3Coption+value%3D%222%22%3EOp%232%3C%2Foption%3E%0A%09%09%3C%2Fselect%3E%0A%09%3C%2Fdiv%3E%0A%3C%2Ffieldset%3E%0A%3Cfieldset+class%3D%22relative%22%3E%0A%09%3Cdiv%3E%0A%09%09%3Ch5%3E%3Cbutton%3ECABIN+%233%3C%2Fbutton%3E%3C%2Fh5%3E%0A%09%3C%2Fdiv%3E%0A%09%3Cdiv%3E%0A%09%09%3Cselect+data-id%3D%22223%22%3E%0A%09%09%09%3Coption+value%3D%221%22%3EOp%231%3C%2Foption%3E%0A%09%09%09%3Coption+value%3D%222%22%3EOp%232%3C%2Foption%3E%0A++++++++++++%3Coption+value%3D%223%22%3EOp%233%3C%2Foption%3E%0A%09%09%3C%2Fselect%3E%0A%09%3C%2Fdiv%3E%0A%3C%2Ffieldset%3E'
   33     1        NEW                                              $13     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $13
   34     4        BEGIN_SILENCE                                    ~16     
          5        INIT_METHOD_CALL                                         !1, 'loadHTML'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        END_SILENCE                                              ~16
   37     9        NEW                                              $18     'DOMXPath'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $18
   39    13        INIT_METHOD_CALL                                         !2, 'query'
         14        SEND_VAL_EX                                              '%2F%2Ffieldset%5B%40class%3D%27relative%27%5D'
         15        DO_FCALL                                      0  $21     
         16        ASSIGN                                                   !3, $21
   40    17        ASSIGN                                                   !4, <array>
   41    18      > FE_RESET_R                                       $24     !3, ->60
         19    > > FE_FETCH_R                                               $24, !5, ->60
   43    20    >   INIT_METHOD_CALL                                         !2, 'query'
         21        SEND_VAL_EX                                              '.%2F%2Ffollowing-sibling%3A%3Ah5%2Fbutton'
         22        SEND_VAR_EX                                              !5
         23        DO_FCALL                                      0  $25     
         24        INIT_METHOD_CALL                                         $25, 'item'
         25        SEND_VAL_EX                                              0
         26        DO_FCALL                                      0  $26     
         27        FETCH_OBJ_R                                      ~27     $26, 'nodeValue'
         28        ASSIGN                                                   !6, ~27
   44    29        INIT_METHOD_CALL                                         !2, 'query'
         30        SEND_VAL_EX                                              '.%2F%2Ffollowing-sibling%3A%3Aselect'
         31        SEND_VAR_EX                                              !5
         32        DO_FCALL                                      0  $29     
         33        ASSIGN                                                   !7, $29
   45    34        ASSIGN_DIM                                               !4, !6
         35        OP_DATA                                                  0
   46    36        FETCH_OBJ_R                                      ~32     !7, 'length'
         37      > JMPZ                                                     ~32, ->59
   47    38    > > FE_RESET_R                                       $33     !7, ->58
         39    > > FE_FETCH_R                                               $33, !8, ->58
   48    40    >   FETCH_OBJ_R                                      ~34     !8, 'childNodes'
         41        ASSIGN                                                   !9, ~34
   50    42        ASSIGN                                                   !10, 1
   51    43      > FE_RESET_R                                       $37     !9, ->56
         44    > > FE_FETCH_R                                               $37, !11, ->56
   52    45    >   FETCH_OBJ_R                                      ~38     !11, 'nodeName'
         46        IS_IDENTICAL                                     ~39     ~38, 'option'
         47      > JMPZ_EX                                          ~39     ~39, ->51
         48    >   FETCH_OBJ_R                                      ~40     !11, 'nodeValue'
         49        IS_NOT_IDENTICAL                                 ~41     ~40, '-'
         50        BOOL                                             ~39     ~41
         51    > > JMPZ                                                     ~39, ->55
   53    52    >   POST_INC                                         ~43     !10
         53        ASSIGN_DIM                                               !4, !6
         54        OP_DATA                                                  ~43
   51    55    > > JMP                                                      ->44
         56    >   FE_FREE                                                  $37
   47    57      > JMP                                                      ->39
         58    >   FE_FREE                                                  $33
   41    59    > > JMP                                                      ->19
         60    >   FE_FREE                                                  $24
   59    61        INIT_FCALL                                               'print_r'
         62        SEND_VAR                                                 !4
         63        DO_ICALL                                                 
         64      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.23 ms | 1014 KiB | 14 Q