3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test( $input) { $doc = new DOMDocument; $doc->loadHTML( $input); $p = $doc->getElementsByTagName('p')->item(0); $children = $p->childNodes; $img_found = false; $error = false; foreach( $children as $child) { echo 'In here'; if( $child->nodeType == XML_TEXT_NODE && !$img_found) { // Text before the image, OK, continue on continue; } if( $child->nodeType == XML_ELEMENT_NODE) { if( !($child->tagName == 'img')) { // echo "Invalid HTML element found"; $error = true; } else { $img_found = true; } } else { // echo "Invalid node type!"; $error = true; } } if( $img_found && !$error) { echo 'ok' . "\n"; } else { echo 'NOT ok' . "\n"; } } $input = '<p><img src="#" /></p>'; // ok test( $input); $input = '<p><img src="#" /><img src="#" /></p>'; // ok test( $input); $input = '<p>xxx<img src="#" /></p>'; // ok test( $input); $input = '<p><img src="#" />xxx</p>'; // NOT ok test( $input);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7m1Fg
function name:  (null)
number of ops:  17
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                   !0, '%3Cp%3E%3Cimg+src%3D%22%23%22+%2F%3E%3C%2Fp%3E'
   34     1        INIT_FCALL                                               'test'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0          
   36     4        ASSIGN                                                   !0, '%3Cp%3E%3Cimg+src%3D%22%23%22+%2F%3E%3Cimg+src%3D%22%23%22+%2F%3E%3C%2Fp%3E'
   37     5        INIT_FCALL                                               'test'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0          
   39     8        ASSIGN                                                   !0, '%3Cp%3Exxx%3Cimg+src%3D%22%23%22+%2F%3E%3C%2Fp%3E'
   40     9        INIT_FCALL                                               'test'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0          
   42    12        ASSIGN                                                   !0, '%3Cp%3E%3Cimg+src%3D%22%23%22+%2F%3Exxx%3C%2Fp%3E'
   43    13        INIT_FCALL                                               'test'
         14        SEND_VAR                                                 !0
         15        DO_FCALL                                      0          
         16      > RETURN                                                   1

Function test:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 41
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 41
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 39
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 26
Branch analysis from position: 41
2 jumps found. (Code = 46) Position 1 = 43, Position 2 = 45
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 41
filename:       /in/7m1Fg
function name:  test
number of ops:  50
compiled vars:  !0 = $input, !1 = $doc, !2 = $p, !3 = $children, !4 = $img_found, !5 = $error, !6 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        NEW                                              $7      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $7
    4     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
    5     7        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
          8        SEND_VAL_EX                                              'p'
          9        DO_FCALL                                      0  $11     
         10        INIT_METHOD_CALL                                         $11, 'item'
         11        SEND_VAL_EX                                              0
         12        DO_FCALL                                      0  $12     
         13        ASSIGN                                                   !2, $12
    6    14        FETCH_OBJ_R                                      ~14     !2, 'childNodes'
         15        ASSIGN                                                   !3, ~14
    7    16        ASSIGN                                                   !4, <false>
         17        ASSIGN                                                   !5, <false>
    8    18      > FE_RESET_R                                       $18     !3, ->41
         19    > > FE_FETCH_R                                               $18, !6, ->41
    9    20    >   ECHO                                                     'In+here'
   10    21        FETCH_OBJ_R                                      ~19     !6, 'nodeType'
         22        IS_EQUAL                                         ~20     ~19, 3
         23      > JMPZ_EX                                          ~20     ~20, ->26
         24    >   BOOL_NOT                                         ~21     !4
         25        BOOL                                             ~20     ~21
         26    > > JMPZ                                                     ~20, ->28
   12    27    > > JMP                                                      ->19
   14    28    >   FETCH_OBJ_R                                      ~22     !6, 'nodeType'
         29        IS_EQUAL                                                 ~22, 1
         30      > JMPZ                                                     ~23, ->39
   15    31    >   FETCH_OBJ_R                                      ~24     !6, 'tagName'
         32        IS_EQUAL                                         ~25     ~24, 'img'
         33        BOOL_NOT                                         ~26     ~25
         34      > JMPZ                                                     ~26, ->37
   17    35    >   ASSIGN                                                   !5, <true>
         36      > JMP                                                      ->38
   19    37    >   ASSIGN                                                   !4, <true>
         38    > > JMP                                                      ->40
   23    39    >   ASSIGN                                                   !5, <true>
    8    40    > > JMP                                                      ->19
         41    >   FE_FREE                                                  $18
   26    42      > JMPZ_EX                                          ~30     !4, ->45
         43    >   BOOL_NOT                                         ~31     !5
         44        BOOL                                             ~30     ~31
         45    > > JMPZ                                                     ~30, ->48
   27    46    >   ECHO                                                     'ok%0A'
         47      > JMP                                                      ->49
   29    48    >   ECHO                                                     'NOT+ok%0A'
   31    49    > > RETURN                                                   null

End of function test

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.24 ms | 1407 KiB | 17 Q