In a PHP script, what regex should I use to check for mismatched parentheses in a string? Things that I want to allow include:
Working php without regex:
function analyse($input){
$len = strlen($input);
$depth = 0;
for ($i = 0; $i < $len; $i++) {
$depth += $input[$i] == '(';
$depth -= $input[$i] == ')';
if ($depth < 0) break;
}
if ($depth != 0) return false;
else return true;
}
$check_nestled = analyse('(5 * 2) + ((2 + 2) - 4)');
if($check_nestled){
// do stuff, everything is ok
}