3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * check matching of braces * * @param string $src target string to check * @return bool */ function brace_check($src) { $open_braces = str_split("({["); $close_braces = str_split(")}]"); $braces = array_merge($open_braces, $close_braces); $tokens = token_get_all($src); // bracesを含むであろう文字列トークン以外は捨てる $tokens = array_filter($tokens,function($item){ return is_string($item); }); $open_brace_stack = array(); foreach($tokens as $key => $t){ if (in_array($t,$open_braces)){ // 開括弧なら開括弧スタックに乗せる array_push( $open_brace_stack, $t); } elseif (in_array($t,$close_braces)){ if ( empty($open_brace_stack) ){ // 開括弧スタックが空ならエラー return false; } // 括弧の対応を確認(開括弧スタックのトップと現在の文字) $open_top = array_pop($open_brace_stack); $idx_open = array_search($open_top, $open_braces); $idx_close = array_search($t, $close_braces); if ( $idx_open !== $idx_close ){ return false; } } } return true; } //========================= // test code $html_case = <<<HTML <?php ?> <html> ... <ul> <li>1)...</li> <li>2)...</li> </ul> ... <?php ?> ... </html> HTML; $expressions = array(")(){}","[]({})","([])","{()[]}","([)]",$html_case,'This is ${great}'); var_dump(array_map('brace_check', $expressions));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tmv8a
function name:  (null)
number of ops:  17
compiled vars:  !0 = $html_case, !1 = $expressions
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, '%3C%3Fphp%0A%3F%3E%0A%3Chtml%3E%0A...%0A%3Cul%3E%0A%3Cli%3E1%29...%3C%2Fli%3E%0A%3Cli%3E2%29...%3C%2Fli%3E%0A%3C%2Ful%3E%0A...%0A%3C%3Fphp%0A%0A%3F%3E%0A...%0A%3C%2Fhtml%3E'
   61     1        INIT_ARRAY                                       ~3      '%29%28%29%7B%7D'
          2        ADD_ARRAY_ELEMENT                                ~3      '%5B%5D%28%7B%7D%29'
          3        ADD_ARRAY_ELEMENT                                ~3      '%28%5B%5D%29'
          4        ADD_ARRAY_ELEMENT                                ~3      '%7B%28%29%5B%5D%7D'
          5        ADD_ARRAY_ELEMENT                                ~3      '%28%5B%29%5D'
          6        ADD_ARRAY_ELEMENT                                ~3      !0
          7        ADD_ARRAY_ELEMENT                                ~3      'This+is+%24%7Bgreat%7D'
          8        ASSIGN                                                   !1, ~3
   63     9        INIT_FCALL                                               'var_dump'
         10        INIT_FCALL                                               'array_map'
         11        SEND_VAL                                                 'brace_check'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $5      
         14        SEND_VAR                                                 $5
         15        DO_ICALL                                                 
         16      > RETURN                                                   1

Function brace_check:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 66
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 66
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 65
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 47
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 65
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
Branch analysis from position: 65
Branch analysis from position: 66
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
filename:       /in/tmv8a
function name:  brace_check
number of ops:  69
compiled vars:  !0 = $src, !1 = $open_braces, !2 = $close_braces, !3 = $braces, !4 = $tokens, !5 = $open_brace_stack, !6 = $t, !7 = $key, !8 = $open_top, !9 = $idx_open, !10 = $idx_close
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        INIT_FCALL                                               'str_split'
          2        SEND_VAL                                                 '%28%7B%5B'
          3        DO_ICALL                                         $11     
          4        ASSIGN                                                   !1, $11
   11     5        INIT_FCALL                                               'str_split'
          6        SEND_VAL                                                 '%29%7D%5D'
          7        DO_ICALL                                         $13     
          8        ASSIGN                                                   !2, $13
   12     9        INIT_FCALL                                               'array_merge'
         10        SEND_VAR                                                 !1
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $15     
         13        ASSIGN                                                   !3, $15
   14    14        INIT_FCALL                                               'token_get_all'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $17     
         17        ASSIGN                                                   !4, $17
   16    18        INIT_FCALL                                               'array_filter'
         19        SEND_VAR                                                 !4
         20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Ftmv8a%3A16%240'
         21        SEND_VAL                                                 ~19
         22        DO_ICALL                                         $20     
         23        ASSIGN                                                   !4, $20
   18    24        ASSIGN                                                   !5, <array>
   19    25      > FE_RESET_R                                       $23     !4, ->66
         26    > > FE_FETCH_R                                       ~24     $23, !6, ->66
         27    >   ASSIGN                                                   !7, ~24
   20    28        INIT_FCALL                                               'in_array'
         29        SEND_VAR                                                 !6
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $26     
         32      > JMPZ                                                     $26, ->38
   22    33    >   INIT_FCALL                                               'array_push'
         34        SEND_REF                                                 !5
         35        SEND_VAR                                                 !6
         36        DO_ICALL                                                 
         37      > JMP                                                      ->65
   24    38    >   INIT_FCALL                                               'in_array'
         39        SEND_VAR                                                 !6
         40        SEND_VAR                                                 !2
         41        DO_ICALL                                         $28     
         42      > JMPZ                                                     $28, ->65
   25    43    >   ISSET_ISEMPTY_CV                                         !5
         44      > JMPZ                                                     ~29, ->47
   27    45    >   FE_FREE                                                  $23
         46      > RETURN                                                   <false>
   30    47    >   INIT_FCALL                                               'array_pop'
         48        SEND_REF                                                 !5
         49        DO_ICALL                                         $30     
         50        ASSIGN                                                   !8, $30
   31    51        INIT_FCALL                                               'array_search'
         52        SEND_VAR                                                 !8
         53        SEND_VAR                                                 !1
         54        DO_ICALL                                         $32     
         55        ASSIGN                                                   !9, $32
   32    56        INIT_FCALL                                               'array_search'
         57        SEND_VAR                                                 !6
         58        SEND_VAR                                                 !2
         59        DO_ICALL                                         $34     
         60        ASSIGN                                                   !10, $34
   33    61        IS_NOT_IDENTICAL                                         !9, !10
         62      > JMPZ                                                     ~36, ->65
   34    63    >   FE_FREE                                                  $23
         64      > RETURN                                                   <false>
   19    65    > > JMP                                                      ->26
         66    >   FE_FREE                                                  $23
   38    67      > RETURN                                                   <true>
   39    68*     > RETURN                                                   null

End of function brace_check

Function %00%7Bclosure%7D%2Fin%2Ftmv8a%3A16%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tmv8a
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        TYPE_CHECK                                   64  ~1      !0
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Ftmv8a%3A16%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.67 ms | 1404 KiB | 33 Q