3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<'HTML' <html> <head> </head> <body id="body"> <div>text1</div> <div>text2</div> </body> </html> HTML; $dom = new DOMDocument(); $dom->loadHtml($html); $xpath = new DOMXpath($dom); $nodes = $xpath->evaluate('//text()[contains(., "text1")]'); foreach ($nodes as $node) { $ancestors = $xpath->evaluate('ancestor::*', $node); $path = ''; foreach ($ancestors as $ancestorNode) { $tagName = $ancestorNode->tagName; $path .= '/'.$tagName; } var_dump($path); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 35
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 35
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 30
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 30
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/sfWEM
function name:  (null)
number of ops:  37
compiled vars:  !0 = $html, !1 = $dom, !2 = $xpath, !3 = $nodes, !4 = $node, !5 = $ancestors, !6 = $path, !7 = $ancestorNode, !8 = $tagName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Chtml%3E%0A++%3Chead%3E%0A++%3C%2Fhead%3E%0A++%3Cbody+id%3D%22body%22%3E%0A++++++%3Cdiv%3Etext1%3C%2Fdiv%3E%0A++++++%3Cdiv%3Etext2%3C%2Fdiv%3E%0A%0A++%3C%2Fbody%3E%0A%3C%2Fhtml%3E'
   15     1        NEW                                              $10     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $10
   16     4        INIT_METHOD_CALL                                         !1, 'loadHtml'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   18     7        NEW                                              $14     'DOMXpath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $14
   20    11        INIT_METHOD_CALL                                         !2, 'evaluate'
         12        SEND_VAL_EX                                              '%2F%2Ftext%28%29%5Bcontains%28.%2C+%22text1%22%29%5D'
         13        DO_FCALL                                      0  $17     
         14        ASSIGN                                                   !3, $17
   21    15      > FE_RESET_R                                       $19     !3, ->35
         16    > > FE_FETCH_R                                               $19, !4, ->35
   22    17    >   INIT_METHOD_CALL                                         !2, 'evaluate'
         18        SEND_VAL_EX                                              'ancestor%3A%3A%2A'
         19        SEND_VAR_EX                                              !4
         20        DO_FCALL                                      0  $20     
         21        ASSIGN                                                   !5, $20
   23    22        ASSIGN                                                   !6, ''
   24    23      > FE_RESET_R                                       $23     !5, ->30
         24    > > FE_FETCH_R                                               $23, !7, ->30
   25    25    >   FETCH_OBJ_R                                      ~24     !7, 'tagName'
         26        ASSIGN                                                   !8, ~24
   26    27        CONCAT                                           ~26     '%2F', !8
         28        ASSIGN_OP                                     8          !6, ~26
   24    29      > JMP                                                      ->24
         30    >   FE_FREE                                                  $23
   29    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !6
         33        DO_ICALL                                                 
   21    34      > JMP                                                      ->16
         35    >   FE_FREE                                                  $19
   30    36      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.12 ms | 1400 KiB | 15 Q