PHP字符串的数学运算

98 阅读1分钟

mathString=goodspricecostprice;mathString = 'goods_price - cost_price'; array = ['goods_price' => 1000, 'cost_price' => 500]; // 解析数学运算字符串 pattern=/([azAZ]+)/;//匹配字母和下划线pregmatchall(pattern = '/([a-zA-Z_]+)/'; // 匹配字母和下划线 preg_match_all(pattern, mathString,mathString, matches); variables=variables = matches[0]; // 获取对应的值 values=[];foreach(values = []; foreach (variables as variable) { if (isset(array[variable])) { values[] = array[array[variable]; } } // 执行数学运算 result=eval("return".strreplace(result = eval("return " . str_replace(variables, values,values, mathString) . ";"); dd($result)