3v4l.org

run code in 300+ PHP versions simultaneously
<?php function validateHtml($html) { $stack = array(); $tags = preg_split("/(<[^>]+>)/", $html, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($tags as $tag) { if (preg_match("/^<\/\s*([^\s>]+)\s*>$/", $tag, $matches)) { // closing tag $last = array_pop($stack); if (!$last || $last !== $matches[1]) { return false; } } elseif (preg_match("/^<\s*([^\s>]+)(\s+[^>]+)?\s*>$/", $tag, $matches)) { // opening tag array_push($stack, $matches[1]); } } return count($stack) === 0; } // example usage $html = "<div><p>Some text</p></div>"; var_dump(validateHtml($html)); // true $html = "<div><p>Some text</div>"; var_dump(validateHtml($html)); // false $html = "<div><p>Some text</p><span></span></div>"; var_dump(validateHtml($html)); // true $html = "<div><p>Some text<span></p></span></div>"; var_dump(validateHtml($html)); // false
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R1aql
function name:  (null)
number of ops:  29
compiled vars:  !0 = $html
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   ASSIGN                                                   !0, '%3Cdiv%3E%3Cp%3ESome+text%3C%2Fp%3E%3C%2Fdiv%3E'
   26     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'validatehtml'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
   28     7        ASSIGN                                                   !0, '%3Cdiv%3E%3Cp%3ESome+text%3C%2Fdiv%3E'
   29     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL                                               'validatehtml'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR                                                 $5
         13        DO_ICALL                                                 
   31    14        ASSIGN                                                   !0, '%3Cdiv%3E%3Cp%3ESome+text%3C%2Fp%3E%3Cspan%3E%3C%2Fspan%3E%3C%2Fdiv%3E'
   32    15        INIT_FCALL                                               'var_dump'
         16        INIT_FCALL                                               'validatehtml'
         17        SEND_VAR                                                 !0
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                                 
   34    21        ASSIGN                                                   !0, '%3Cdiv%3E%3Cp%3ESome+text%3Cspan%3E%3C%2Fp%3E%3C%2Fspan%3E%3C%2Fdiv%3E'
   35    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'validatehtml'
         24        SEND_VAR                                                 !0
         25        DO_FCALL                                      0  $11     
         26        SEND_VAR                                                 $11
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function validatehtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 42
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 42
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 30
Branch analysis from position: 17
2 jumps found. (Code = 47) Position 1 = 23, Position 2 = 26
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 29
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 26
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 41
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/R1aql
function name:  validateHtml
number of ops:  47
compiled vars:  !0 = $html, !1 = $stack, !2 = $tags, !3 = $tag, !4 = $matches, !5 = $last
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, <array>
    6     2        INIT_FCALL                                               'preg_split'
          3        SEND_VAL                                                 '%2F%28%3C%5B%5E%3E%5D%2B%3E%29%2F'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 -1
          6        SEND_VAL                                                 2
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
    8     9      > FE_RESET_R                                       $9      !2, ->42
         10    > > FE_FETCH_R                                               $9, !3, ->42
    9    11    >   INIT_FCALL                                               'preg_match'
         12        SEND_VAL                                                 '%2F%5E%3C%5C%2F%5Cs%2A%28%5B%5E%5Cs%3E%5D%2B%29%5Cs%2A%3E%24%2F'
         13        SEND_VAR                                                 !3
         14        SEND_REF                                                 !4
         15        DO_ICALL                                         $10     
         16      > JMPZ                                                     $10, ->30
   11    17    >   INIT_FCALL                                               'array_pop'
         18        SEND_REF                                                 !1
         19        DO_ICALL                                         $11     
         20        ASSIGN                                                   !5, $11
   12    21        BOOL_NOT                                         ~13     !5
         22      > JMPNZ_EX                                         ~13     ~13, ->26
         23    >   FETCH_DIM_R                                      ~14     !4, 1
         24        IS_NOT_IDENTICAL                                 ~15     !5, ~14
         25        BOOL                                             ~13     ~15
         26    > > JMPZ                                                     ~13, ->29
   13    27    >   FE_FREE                                                  $9
         28      > RETURN                                                   <false>
         29    > > JMP                                                      ->41
   15    30    >   INIT_FCALL                                               'preg_match'
         31        SEND_VAL                                                 '%2F%5E%3C%5Cs%2A%28%5B%5E%5Cs%3E%5D%2B%29%28%5Cs%2B%5B%5E%3E%5D%2B%29%3F%5Cs%2A%3E%24%2F'
         32        SEND_VAR                                                 !3
         33        SEND_REF                                                 !4
         34        DO_ICALL                                         $16     
         35      > JMPZ                                                     $16, ->41
   17    36    >   INIT_FCALL                                               'array_push'
         37        SEND_REF                                                 !1
         38        FETCH_DIM_R                                      ~17     !4, 1
         39        SEND_VAL                                                 ~17
         40        DO_ICALL                                                 
    8    41    > > JMP                                                      ->10
         42    >   FE_FREE                                                  $9
   21    43        COUNT                                            ~19     !1
         44        IS_IDENTICAL                                     ~20     ~19, 0
         45      > RETURN                                                   ~20
   22    46*     > RETURN                                                   null

End of function validatehtml

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.98 ms | 1411 KiB | 27 Q