3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<'HTML' <p>Lorem ipsum dolor sit amet...</p> <p>consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat</p> <img src="/path/to/image.jpg" /> <h2 class="style" id="name">first headline 2</h2> <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat</p> <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat</p> <h3 class="style" id="name1">Headline 3</h3> <p>vel illum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui</p> <h3 class="style" id="name2">On more Headline 3</h3> <p>blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi</p> <h2 id="name3" class="style">Headline 2 with class</h2> <p>Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet</p> <H2 id="name4">Another Headline 2 without class</h2> <p>doming id quod mazim placerat facer possim assum</p> HTML; libxml_use_internal_errors(true); $dom = new DOMDocument; $dom->loadHTML($html); $xp = new DOMXPath($dom); $query = '//*[contains("h1 h2 h3 h4 h5 h6", name())]'; $nodes = $xp->query($query); $currentLevel = ['level' => 0, 'count' => 0]; $stack = []; $format = '<li><a href="#%s">%s</a></li>'; $result = ''; foreach($nodes as $node) { $level = (int)$node->tagName[1]; // extract the digit after h while($level < $currentLevel['level']) { $currentLevel = array_pop($stack); $result .= '</ul>'; } if ($level === $currentLevel['level']) { $currentLevel['count']++; } else { $stack[] = $currentLevel; $currentLevel = ['level' => $level, 'count' => 1]; $result .= '<ul>'; } $result .= sprintf($format, $node->getAttribute('id'), $node->nodeValue); } $result .= str_repeat('</ul>', count($stack)); echo $result;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 61
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 61
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 30
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 44
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 30
Branch analysis from position: 38
Branch analysis from position: 30
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
filename:       /in/MWJrel
function name:  (null)
number of ops:  70
compiled vars:  !0 = $html, !1 = $dom, !2 = $xp, !3 = $query, !4 = $nodes, !5 = $currentLevel, !6 = $stack, !7 = $format, !8 = $result, !9 = $node, !10 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cp%3ELorem+ipsum+dolor+sit+amet...%3C%2Fp%3E%0A%3Cp%3Econsectetuer+adipiscing+elit%2C+sed+diam+nonummy+nibh+euismod+tincidunt+ut+laoreet+dolore+magna+aliquam+erat+volutpat%3C%2Fp%3E%0A%3Cimg+src%3D%22%2Fpath%2Fto%2Fimage.jpg%22+%2F%3E%0A%3Ch2+class%3D%22style%22+id%3D%22name%22%3Efirst+headline+2%3C%2Fh2%3E%0A%3Cp%3EUt+wisi+enim+ad+minim+veniam%2C+quis+nostrud+exerci+tation+ullamcorper+suscipit+lobortis+nisl+ut+aliquip+ex+ea+commodo+consequat%3C%2Fp%3E%0A%3Cp%3EDuis+autem+vel+eum+iriure+dolor+in+hendrerit+in+vulputate+velit+esse+molestie+consequat%3C%2Fp%3E+%0A%3Ch3+class%3D%22style%22+id%3D%22name1%22%3EHeadline+3%3C%2Fh3%3E%0A%3Cp%3Evel+illum+dolore+eu+feugiat+nulla+facilisis+at+vero+et+accumsan+et+iusto+odio+dignissim+qui%3C%2Fp%3E%0A%3Ch3+class%3D%22style%22+id%3D%22name2%22%3EOn+more+Headline+3%3C%2Fh3%3E%0A%3Cp%3Eblandit+praesent+luptatum+zzril+delenit+augue+duis+dolore+te+feugait+nulla+facilisi%3C%2Fp%3E%0A%3Ch2+id%3D%22name3%22+class%3D%22style%22%3EHeadline+2+with+class%3C%2Fh2%3E%0A%3Cp%3ENam+liber+tempor+cum+soluta+nobis+eleifend+option+congue+nihil+imperdiet%3C%2Fp%3E%0A%3CH2+id%3D%22name4%22%3EAnother+Headline+2+without+class%3C%2Fh2%3E%0A%3Cp%3Edoming+id+quod+mazim+placerat+facer+possim+assum%3C%2Fp%3E'
   20     1        INIT_FCALL                                               'libxml_use_internal_errors'
          2        SEND_VAL                                                 <true>
          3        DO_ICALL                                                 
   22     4        NEW                                              $13     'DOMDocument'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $13
   23     7        INIT_METHOD_CALL                                         !1, 'loadHTML'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
   25    10        NEW                                              $17     'DOMXPath'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !2, $17
   26    14        ASSIGN                                                   !3, '%2F%2F%2A%5Bcontains%28%22h1+h2+h3+h4+h5+h6%22%2C+name%28%29%29%5D'
   28    15        INIT_METHOD_CALL                                         !2, 'query'
         16        SEND_VAR_EX                                              !3
         17        DO_FCALL                                      0  $21     
         18        ASSIGN                                                   !4, $21
   30    19        ASSIGN                                                   !5, <array>
   31    20        ASSIGN                                                   !6, <array>
   32    21        ASSIGN                                                   !7, '%3Cli%3E%3Ca+href%3D%22%23%25s%22%3E%25s%3C%2Fa%3E%3C%2Fli%3E'
   33    22        ASSIGN                                                   !8, ''
   35    23      > FE_RESET_R                                       $27     !4, ->61
         24    > > FE_FETCH_R                                               $27, !9, ->61
   36    25    >   FETCH_OBJ_R                                      ~28     !9, 'tagName'
         26        FETCH_DIM_R                                      ~29     ~28, 1
         27        CAST                                          4  ~30     ~29
         28        ASSIGN                                                   !10, ~30
   38    29      > JMP                                                      ->35
   39    30    >   INIT_FCALL                                               'array_pop'
         31        SEND_REF                                                 !6
         32        DO_ICALL                                         $32     
         33        ASSIGN                                                   !5, $32
   40    34        ASSIGN_OP                                     8          !8, '%3C%2Ful%3E'
   38    35    >   FETCH_DIM_R                                      ~35     !5, 'level'
         36        IS_SMALLER                                               !10, ~35
         37      > JMPNZ                                                    ~36, ->30
   43    38    >   FETCH_DIM_R                                      ~37     !5, 'level'
         39        IS_IDENTICAL                                             !10, ~37
         40      > JMPZ                                                     ~38, ->44
   44    41    >   FETCH_DIM_RW                                     $39     !5, 'count'
         42        PRE_INC                                                  $39
   43    43      > JMP                                                      ->50
   46    44    >   ASSIGN_DIM                                               !6
         45        OP_DATA                                                  !5
   47    46        INIT_ARRAY                                       ~42     !10, 'level'
         47        ADD_ARRAY_ELEMENT                                ~42     1, 'count'
         48        ASSIGN                                                   !5, ~42
   48    49        ASSIGN_OP                                     8          !8, '%3Cul%3E'
   51    50    >   INIT_FCALL                                               'sprintf'
         51        SEND_VAR                                                 !7
         52        INIT_METHOD_CALL                                         !9, 'getAttribute'
         53        SEND_VAL_EX                                              'id'
         54        DO_FCALL                                      0  $45     
         55        SEND_VAR                                                 $45
         56        FETCH_OBJ_R                                      ~46     !9, 'nodeValue'
         57        SEND_VAL                                                 ~46
         58        DO_ICALL                                         $47     
         59        ASSIGN_OP                                     8          !8, $47
   35    60      > JMP                                                      ->24
         61    >   FE_FREE                                                  $27
   54    62        INIT_FCALL                                               'str_repeat'
         63        SEND_VAL                                                 '%3C%2Ful%3E'
         64        COUNT                                            ~49     !6
         65        SEND_VAL                                                 ~49
         66        DO_ICALL                                         $50     
         67        ASSIGN_OP                                     8          !8, $50
   56    68        ECHO                                                     !8
   57    69      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.2 ms | 1025 KiB | 17 Q