考虑到 PHP 中提供的各种数学函数,我会质疑使用 eval。您说过您只想做简单的数学运算——使用eval 的唯一原因是执行更复杂的运算,或者从用户那里完全接受方程式。
如果您只想加减,请使用intval 清理输入并前往城镇:
$number1 = '100';
$number2 = 'shell_exec(\'rm -rf *\')';
echo intval($number1) + intval($number2); // 100
试试看:http://codepad.org/LSUDUw1M
这是因为intval 会忽略任何非数字。
如果您确实从用户输入(即100 - 20)中获得了整个方程式,则可以使用preg_replace 删除除允许的运算符和数字之外的任何内容:
$input = '20 + 4; shell_exec(\'rm *\')';
$input = preg_replace(
'/[^0-9+-]/',
'',
$input
);
eval('$result = '.$input.';');
echo 'result: '.$result; // 24
试试看:http://codepad.org/tnISDPJ3
在这里,我们使用的是正则表达式/[^0-9+-]/,它匹配任何非 0-9 OR + OR - 并将其替换为空字符串。
如果您想更深入地了解允许的方程式,请直接从eval 手册页获取:
// credit for code to bohwaz (http://www.php.net/manual/en/function.eval.php#107377)
$test = '2+3*pi';
// Remove whitespaces
$test = preg_replace('/\s+/', '', $test);
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
$functions = '(?:abs|a?cosh?|a?sinh?|a?tanh?|exp|log10|deg2rad|rad2deg|sqrt|ceil|floor|round)'; // Allowed PHP functions
$operators = '[+\/*^%-]'; // Allowed math operators
$regexp = '/^(('.$number.'|'.$functions.'\s*\((?1)+\)|\((?1)+\))(?:'.$operators.'(?2))?)+$/'; // Final regexp, heavily using recursive patterns
if (preg_match($regexp, $q))
{
$test = preg_replace('!pi|π!', 'pi()', $test); // Replace pi with pi function
eval('$result = '.$test.';');
}
else
{
$result = false;
}
文档