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));
Output for git.master, git.master_jit, rfc.property-hooks
array(7) { [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) [3]=> bool(true) [4]=> bool(true) [5]=> bool(true) [6]=> bool(true) }

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:
56.84 ms | 401 KiB | 8 Q