如何在字符串中找到运算符并根据我的公式计算?
how to find operator in sting and calculate as per my formula?
我的数据库中存储了字符串格式的公式。我只想计算通过成本值。
$cost = 5;
$formula = '{{cost}} * 1.5 + 3';
$formula = str_replace('{{cost}}', $cost, $formula);
您可以在代码行后使用 eval
。
echo eval('return '.$formula.';');
Please keep in mind that Using eval
function is very dangerous when you can't control the string argument.
我的数据库中存储了字符串格式的公式。我只想计算通过成本值。
$cost = 5;
$formula = '{{cost}} * 1.5 + 3';
$formula = str_replace('{{cost}}', $cost, $formula);
您可以在代码行后使用 eval
。
echo eval('return '.$formula.';');
Please keep in mind that Using
eval
function is very dangerous when you can't control the string argument.