我从表单字段中获得以下信息:
只有一次计算公式
(z + a * (s-k)) * d + (m * x)
以及包含此信息的七个文本(输入)字段:
value => 0.5
name => a
value => 10.2
name => z
value => 4
name => x
value => 2
name => s
value => 2
name => d
value => 0.4
name => k
value => 2.1
name => m
根据此信息,我希望将此结果作为计算文本:
(10.2 + 0.5 * (2-0.4)) * 2+ (2.1 * 4)
我的实验如下所示:
$sFields => stdClass Object (
[price] => 0.5
[letter] => a
[price] => 10.2
[letter] => z
[price] => 4
[letter] => x
[price] => 2
[letter] => s
[price] => 2
[letter] => d
[price] => 0.4
[letter] => k
[price] => 2.1
[letter] => m
)
$sFormula = $this->formRequestParameter("(z + a * (s-k)) * d + (m * x)");
protected function _calculateFormFields($sFields, $sFormula)
{
$formulaString = preg_replace("/[^a-zA-Z0-9]+/", ',', strtolower($sFormula));
$exLetter = explode(',', $formulaString);
$sPrice = null;
foreach ($exLetter as $sKey => $sVal) {
if (strtolower($sVal) == strtolower($sFields->letter)) {
$sPrice[$sKey] = $sFields->price;
}
}
$buildFormula = str_replace(
$exLetter,
$sPrice,
$sFormula
);
print_r($buildFormula);
// $this->_calculatePrice($buildFormula);
}
输出看起来像这样。:-(
10.2 (+*(-))*+(*)
4 (+*(-))*+(*)
2.1 (+*(-))*+(*)
2 (+*(-))*+(*)
2 (+*(-))*+(*)
0.5 (+*(-))*+(*)
0.4 (+*(-))*+(*)