3v4l.org

run code in 300+ PHP versions simultaneously
<?php $headers = <<<'HTML' <html> <h5>5 Header 0.0.0.0.1</h5> <h1>1 Header 1</h1> <h2>2 Header 1.1</h2> <h2>2 Header 1.2</h2> <h3>3 Header 1.2.1</h3> <h4>4 Header 1.2.2</h4> <h2>2 Header 1.3</h2> <h3>3 Header 1.3.1</h3> <h3>3 Header 1.3.2</h3> <h5>5 Header 1.3.2.1.1</h5> <h6>6 Header 1.3.2.1.1.1</h6> <h1>1 Header 2</h1> <h2>2 Header 2.1</h2> </html> HTML; $doc = new DomDocument(); $doc->loadHTML($headers); $xpath = new DOMXPath($doc); $otag = '<ul>'; $ctag = '</ul>'; $query = '//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6]'; $previousLevel = 1; $out[] = $otag; foreach ($xpath->query($query) as $headerElement) { $level = (int) $headerElement->nodeName[1]; if ($level < 1 || $level > 6) { $level = 1; continue; } if ($level > $previousLevel) { $out[] = str_repeat($otag, $level - $previousLevel); } elseif ($level < $previousLevel) { $out[] = str_repeat($ctag, $previousLevel - $level); } $out[] = '<li>' . $headerElement->textContent . '</li>'; $previousLevel = $level; } $out[] = str_repeat($ctag, $level); echo implode(PHP_EOL, $out);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 59
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 59
Branch analysis from position: 22
2 jumps found. (Code = 47) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 43
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 52
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 52
Branch analysis from position: 30
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
filename:       /in/Z28s4
function name:  (null)
number of ops:  72
compiled vars:  !0 = $headers, !1 = $doc, !2 = $xpath, !3 = $otag, !4 = $ctag, !5 = $query, !6 = $previousLevel, !7 = $out, !8 = $headerElement, !9 = $level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Chtml%3E%0A%3Ch5%3E5+Header+0.0.0.0.1%3C%2Fh5%3E%0A%3Ch1%3E1+Header+1%3C%2Fh1%3E%0A%3Ch2%3E2+Header+1.1%3C%2Fh2%3E%0A%3Ch2%3E2+Header+1.2%3C%2Fh2%3E%0A%3Ch3%3E3+Header+1.2.1%3C%2Fh3%3E%0A%3Ch4%3E4+Header+1.2.2%3C%2Fh4%3E%0A%3Ch2%3E2+Header+1.3%3C%2Fh2%3E%0A%3Ch3%3E3+Header+1.3.1%3C%2Fh3%3E%0A%3Ch3%3E3+Header+1.3.2%3C%2Fh3%3E%0A%3Ch5%3E5+Header+1.3.2.1.1%3C%2Fh5%3E%0A%3Ch6%3E6+Header+1.3.2.1.1.1%3C%2Fh6%3E%0A%3Ch1%3E1+Header+2%3C%2Fh1%3E%0A%3Ch2%3E2+Header+2.1%3C%2Fh2%3E%0A%3C%2Fhtml%3E'
   21     1        NEW                                              $11     'DomDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $11
   22     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   23     7        NEW                                              $15     'DOMXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $15
   25    11        ASSIGN                                                   !3, '%3Cul%3E'
   26    12        ASSIGN                                                   !4, '%3C%2Ful%3E'
   27    13        ASSIGN                                                   !5, '%2F%2F%2A%5Bself%3A%3Ah1+or+self%3A%3Ah2+or+self%3A%3Ah3+or+self%3A%3Ah4+or+self%3A%3Ah5+or+self%3A%3Ah6%5D'
   28    14        ASSIGN                                                   !6, 1
   29    15        ASSIGN_DIM                                               !7
         16        OP_DATA                                                  !3
   31    17        INIT_METHOD_CALL                                         !2, 'query'
         18        SEND_VAR_EX                                              !5
         19        DO_FCALL                                      0  $23     
         20      > FE_RESET_R                                       $24     $23, ->59
         21    > > FE_FETCH_R                                               $24, !8, ->59
   32    22    >   FETCH_OBJ_R                                      ~25     !8, 'nodeName'
         23        FETCH_DIM_R                                      ~26     ~25, 1
         24        CAST                                          4  ~27     ~26
         25        ASSIGN                                                   !9, ~27
   34    26        IS_SMALLER                                       ~29     !9, 1
         27      > JMPNZ_EX                                         ~29     ~29, ->30
         28    >   IS_SMALLER                                       ~30     6, !9
         29        BOOL                                             ~29     ~30
         30    > > JMPZ                                                     ~29, ->33
   35    31    >   ASSIGN                                                   !9, 1
   36    32      > JMP                                                      ->21
   39    33    >   IS_SMALLER                                               !6, !9
         34      > JMPZ                                                     ~32, ->43
   40    35    >   INIT_FCALL                                               'str_repeat'
         36        SEND_VAR                                                 !3
         37        SUB                                              ~34     !9, !6
         38        SEND_VAL                                                 ~34
         39        DO_ICALL                                         $35     
         40        ASSIGN_DIM                                               !7
         41        OP_DATA                                                  $35
   39    42      > JMP                                                      ->52
   41    43    >   IS_SMALLER                                               !9, !6
         44      > JMPZ                                                     ~36, ->52
   42    45    >   INIT_FCALL                                               'str_repeat'
         46        SEND_VAR                                                 !4
         47        SUB                                              ~38     !6, !9
         48        SEND_VAL                                                 ~38
         49        DO_ICALL                                         $39     
         50        ASSIGN_DIM                                               !7
         51        OP_DATA                                                  $39
   45    52    >   FETCH_OBJ_R                                      ~41     !8, 'textContent'
         53        CONCAT                                           ~42     '%3Cli%3E', ~41
         54        CONCAT                                           ~43     ~42, '%3C%2Fli%3E'
         55        ASSIGN_DIM                                               !7
         56        OP_DATA                                                  ~43
   46    57        ASSIGN                                                   !6, !9
   31    58      > JMP                                                      ->21
         59    >   FE_FREE                                                  $24
   49    60        INIT_FCALL                                               'str_repeat'
         61        SEND_VAR                                                 !4
         62        SEND_VAR                                                 !9
         63        DO_ICALL                                         $46     
         64        ASSIGN_DIM                                               !7
         65        OP_DATA                                                  $46
   51    66        INIT_FCALL                                               'implode'
         67        SEND_VAL                                                 '%0A'
         68        SEND_VAR                                                 !7
         69        DO_ICALL                                         $47     
         70        ECHO                                                     $47
         71      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
198.99 ms | 1008 KiB | 15 Q