<?phpclass Solution { /** * '(()' -> 2 * ')()())' -> 4 * @param $s * @return int|mixed */ public function longestValidParentheses($s) { $max = 0; $start = 0; $stack = new SplStack(); for ($i = 0; $i < strlen($s); $i++) { if ($s[$i] == '(') { $stack->push($i); } else { if ($stack->isEmpty()) { $start = $i + 1; } else { $stack->pop(); $max = $stack->isEmpty() ? max($max, $i - $start + 1) : max($max, $i - $stack->top()); } } } return $max; }}$s = ')(((()())(';$cls = new Solution();$r = $cls->longestValidParentheses($s);echo $r;