function filterEquationBrackets($q) { $serializedEquation = serializeEquationToArray($q); $equation = deserializeArrayToEquation($serializedEquation); $filteredSerializedEquation = filterBracketBySerializedEquation($serializedEquation); return deserializeArrayToEquation($filteredSerializedEquation); }
function serializeEquationToArray($equation) { // rm unnecessary spaces and brackets $equation = str_replace(' ', '', $equation); $equation = str_replace('()', '', $equation); // remove brackets if there is only one operator between brackets $equation = preg_replace('/(\\()([\\+\\-\\*\\/]+)(\\))/', '$2', $equation); // add '*' if no opeator between number and bracket $equation = preg_replace('/(\\w|\\))(\\()/', '$1*(', $equation); $equation = preg_replace('/(\\))(\\w)/', ')*$2', $equation); // check total open and close bracket count, alert if not equal $totalOpenBracketCount = preg_match_all('/\\(/', $equation, $a); $totalCloseBracketCount = preg_match_all('/\\)/', $equation); if ($totalOpenBracketCount != $totalCloseBracketCount) { echo '<script>alert("invalid equation");</script>'; exit; } // init parameters $rt = array(); $openBracketCount = 0; $closeBracketCount = 0; $openBracketPos = 0; $closeBracketPos = 0; // split $equation to characters $characters = str_split($equation); // process foreach ($characters as $key => $c) { // record the open bracket position if ($c == '(') { $openBracketCount++; // only record the first open bracket position if ($openBracketCount == 1) { $openBracketPos = $key; } continue; } // if no bracket has been recorded, push the character into array if ($openBracketCount == 0) { $rt[] = $c; continue; } // record the close bracket position if ($c == ')') { $closeBracketCount++; $closeBracketPos = $key; // if count of open and close bracket are equal, try to get sub equation if ($openBracketCount == $closeBracketCount) { // get sub equation $startPos = $openBracketPos + 1; $endPos = $closeBracketPos - 1; $subEquation = ''; for ($i = $startPos; $i <= $endPos; $i++) { $subEquation .= $characters[$i]; } // serilize sub equation to array $subRt = array('('); $subRt = array_merge($subRt, serializeEquationToArray($subEquation)); $subRt[] = ')'; $rt[] = $subRt; // reset open and close bracket count $openBracketCount = 0; $closeBracketCount = 0; } } } return $rt; }