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 git.master, git.master_jit
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, bool given in /in/8lPcf on line 21 1
Output for rfc.property-hooks
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

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
59.39 ms | 405 KiB | 9 Q