3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<EOT <body> <div class="page-content"> First text <p>Second text</p> <div>Third text</div> <p>More text</p> <h4>Subtitle 1</h4> <p>bla bla</p> <p>bla bla</p> <h4>Subtitle 2</h4> <p>bla bla</p> <p>bla bla</p> </div> </body> EOT; $doc = new DOMDocument(); $doc->loadHTML($html); // Get our node by class name // See https://stackoverflow.com/a/6366390/231316 $finder = new DomXPath($doc); $classname = "page-content"; $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); $buf = ''; foreach ($nodes as $node) { foreach ($node->childNodes as $child) { if ($child->nodeName === 'h4') { break; } $buf .= $doc->saveHTML($child); } } echo $buf;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 36
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 36
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 34
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 34
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 34
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/JWUi5
function name:  (null)
number of ops:  39
compiled vars:  !0 = $html, !1 = $doc, !2 = $finder, !3 = $classname, !4 = $nodes, !5 = $buf, !6 = $node, !7 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cbody%3E%0A%3Cdiv+class%3D%22page-content%22%3E%0A+++First+text%0A+++%3Cp%3ESecond+text%3C%2Fp%3E%0A+++%3Cdiv%3EThird+text%3C%2Fdiv%3E%0A+++%3Cp%3EMore+text%3C%2Fp%3E%0A+++%3Ch4%3ESubtitle+1%3C%2Fh4%3E%0A+++%3Cp%3Ebla+bla%3C%2Fp%3E%0A+++%3Cp%3Ebla+bla%3C%2Fp%3E%0A+++%3Ch4%3ESubtitle+2%3C%2Fh4%3E%0A+++%3Cp%3Ebla+bla%3C%2Fp%3E%0A+++%3Cp%3Ebla+bla%3C%2Fp%3E%0A%3C%2Fdiv%3E%0A%3C%2Fbody%3E'
   20     1        NEW                                              $9      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $9
   21     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   25     7        NEW                                              $13     'DomXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $13
   26    11        ASSIGN                                                   !3, 'page-content'
   27    12        INIT_METHOD_CALL                                         !2, 'query'
         13        ROPE_INIT                                     3  ~18     '%2F%2F%2A%5Bcontains%28concat%28%27+%27%2C+normalize-space%28%40class%29%2C+%27+%27%29%2C+%27+'
         14        ROPE_ADD                                      1  ~18     ~18, !3
         15        ROPE_END                                      2  ~17     ~18, '+%27%29%5D'
         16        SEND_VAL_EX                                              ~17
         17        DO_FCALL                                      0  $20     
         18        ASSIGN                                                   !4, $20
   29    19        ASSIGN                                                   !5, ''
   30    20      > FE_RESET_R                                       $23     !4, ->36
         21    > > FE_FETCH_R                                               $23, !6, ->36
   31    22    >   FETCH_OBJ_R                                      ~24     !6, 'childNodes'
         23      > FE_RESET_R                                       $25     ~24, ->34
         24    > > FE_FETCH_R                                               $25, !7, ->34
   32    25    >   FETCH_OBJ_R                                      ~26     !7, 'nodeName'
         26        IS_IDENTICAL                                             ~26, 'h4'
         27      > JMPZ                                                     ~27, ->29
   33    28    > > JMP                                                      ->34
   35    29    >   INIT_METHOD_CALL                                         !1, 'saveHTML'
         30        SEND_VAR_EX                                              !7
         31        DO_FCALL                                      0  $28     
         32        ASSIGN_OP                                     8          !5, $28
   31    33      > JMP                                                      ->24
         34    >   FE_FREE                                                  $25
   30    35      > JMP                                                      ->21
         36    >   FE_FREE                                                  $23
   39    37        ECHO                                                     !5
   40    38      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.71 ms | 1074 KiB | 13 Q