如何读取/解密用户定义的数学公式并在php中执行请求的操作

I have a user defined formula stored in mysql table as AVG({Arrival_Date}-{Departure_Date}) I managed to get items Arrival_Date and Departure_Date then select the rows from a separate table.

How can I replace {Arrival_Date} and {Departure_Date} from AVG({Arrival_Date}-{Departure_Date}) with the actual data selected from database table?

Hi thanks all for your suggestion. I solved the above using https://github.com/jlawrence11/eos. So there is no need of trying to decipher the formula as long as it follows bodmas rule.

You only read the formula as defined, pass the values(numbers or floats) to the formula and then call eos to do the computation.

Example:

  <?php

      $formula_as_defined = AVG({Arrival_Date}-{Departure_Date})  ; //get the formula from database

      $str= str_ireplace('{','',$formula_as_defined);
      $mod_str= str_ireplace('}','',$str);
      $mod_str;
      //
 foreach($sql_select_resultset as $array) //This an array of values selected from db table #1 => Arrival_Date #2 =>Departure_Date
              {
                  foreach($array as $key => $value)
                  {
                      $col_mod[]= $key;

                      $val_mod[]= strtotime($value);
                      //
                  }
                    $char_mod = str_ireplace($col_mod,$val_mod,$mod_str);
                    unset($col_mod);
                    unset($val_mod);
                    //
                  //   $equation = "(".strtotime('13-07-2015')."-".strtotime('20-07-2015').")";
                     $equation = $char_mod  ;
                     $eq = new eqEOS();
                     $result = $eq->solveIF($equation); //pass the your formula to solveIF and your are done.
                     //
                     $myresults = $this->groupData($myresults,$array,$x_axis_column,$results_type_time);
                     array_push($myresults[date('m',strtotime($array[$x_axis_column]))],$result);

              }
     ?>