fork(1) download
  1. <?php
  2. function callback1($m) {return c($m[1]);}
  3. function callback2($n,$m) {
  4. $o=$m[0];
  5. $m[0]=' ';
  6. return $o=='+' ? $n+$m : ($o=='-' ? $n-$m : ($o=='*' ? $n*$m : $n/$m));
  7. }
  8. function c($s){
  9. while ($s != ($t = preg_replace_callback('/\(([^()]*)\)/','callback1',$s))) $s=$t;
  10. preg_match_all('![-+/*].*?[\d.]+!', "+$s", $m);
  11. return array_reduce($m[0], 'callback2');
  12. }
  13.  
  14. $str = '(3 + 2) * 5';
  15. echo c($str);
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
25