3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * check matching od 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); var_dump($tokens); //$tokens = array_filter($tokens,function($item){ return is_string($item); }); var_dump($tokens); $tokens = array_walk_recursive($tokens,function(&$item){ if (is_int($item)) $item = token_name($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; } } echo "token:$t" . PHP_EOL; echo "open_brace_stack:" . print_r($open_brace_stack,true) . PHP_EOL; } return true; } //========================= // test code $html_case = <<<HTML <?php ?> <html> ... <ul> <li>1)...</li> <li>2)...</li> </ul> ... <?php ... ?> ... </html> HTML; $expressions = array(")(){}","[]({})","([])","{()[]}","([)]",$html_case); //var_dump(array_map('brace_check', $expressions)); echo brace_check("([)]");
Output for 8.3.1, 8.3.3, 8.3.6
array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: foreach() argument must be of type array|object, true given in /in/8lPcf on line 21 1
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: foreach() argument must be of type array|object, true given in /in/8lPcf on line 21 1
Output for 8.3.0, 8.3.2, 8.3.4
array(1) { [0]=> array(3) { [0]=> int(318) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(318) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: foreach() argument must be of type array|object, true given in /in/8lPcf on line 21 1
Output for 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(267) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: foreach() argument must be of type array|object, bool given in /in/8lPcf on line 21 1
Output for 8.0.0 - 8.0.29
array(1) { [0]=> array(3) { [0]=> int(316) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(316) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: foreach() argument must be of type array|object, bool given in /in/8lPcf on line 21 1
Output for 7.4.0 - 7.4.33
array(1) { [0]=> array(3) { [0]=> int(313) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(313) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: Invalid argument supplied for foreach() in /in/8lPcf on line 21 1
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
array(1) { [0]=> array(3) { [0]=> int(321) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(321) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: Invalid argument supplied for foreach() in /in/8lPcf on line 21 1
Output for 5.6.8 - 5.6.28
array(1) { [0]=> array(3) { [0]=> int(314) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(314) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: Invalid argument supplied for foreach() in /in/8lPcf on line 21 1
Output for 5.5.24 - 5.5.35
array(1) { [0]=> array(3) { [0]=> int(312) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(312) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: Invalid argument supplied for foreach() in /in/8lPcf on line 21 1
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45
array(1) { [0]=> array(3) { [0]=> int(311) [1]=> string(4) "([)]" [2]=> int(1) } } array(1) { [0]=> array(3) { [0]=> int(311) [1]=> string(4) "([)]" [2]=> int(1) } } Warning: Invalid argument supplied for foreach() in /in/8lPcf on line 21 1
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/8lPcf on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/8lPcf on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/8lPcf on line 18
Process exited with code 255.

preferences:
228.9 ms | 401 KiB | 342 Q