These days nothing is written in PHP with a parser program.
For example: (21*12-23-(21+14-(21-31/3+ (14-21*12-14)) +54)-21) +21*25-22*26
Then the result of the analysis is:-169.33.
The code is now posted, if there is a bad place also ask the programmer friends to point out.
if (Isset ($_get[' Sizhi ')) {$sizhi =$_get[' Sizhi ']; if (!checksizhi ($sizhi)) {echo (' The formula is not valid, check if the parentheses are paired '); }else{$sizhi =priority ($sizhi); Echo (Calculate ($sizhi)); }}//Check whether the calculation is legal function Checksizhi ($sizhi) {$Amatch =array (); $pattern = '/((\d|!) \()| ([-\+\*\/]\))| [^-\+\*\/!\d\ (\)]/'; if (Preg_match ($pattern, $sizhi, $Amatch) >0) {echo (' illegal element: '); Print_r ($Amatch); return false; }else{$kuohao = 0; For ($i =0, $k =strlen ($sizhi), $i < $k, $i + +) {if ($sizhi [$i]== "(") {$kuohao + +); } if ($sizhi [$i]== ")") {$kuohao-; if ($kuohao <0) return false; }} if ($kuohao!=0) return false; return true; }}//gets the priority to handle the contents of the parentheses in the function priorities ($sizhi, $start =0) {for ($i = $start; $i
0) {echo ("factorial phase problem, please change the formula"); Exit (); }else{//Replaces part of all factorial $sizhi =preg_replace_callback ($par, function ($siz) {return calculatefactorial ($siz [0]); }, $sizhi); //Exchange symbols and numbers and calculate return change ($sizhi);} Calculates the factorial function calculatefactorial ($sizhi) {for ($i =0; $i
0) {$str = $array [0]; } return $STR; }}
The above describes the calculation of the analytic algorithm, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.