使用eval从字符串计算数学表达式

问题描述 投票:18回答:9

我想从字符串中计算数学表达式。我已经读过,解决方法是使用eval()。但是当我尝试运行以下代码时:

<?php

$ma ="2+10";
$p = eval($ma);
print $p;

?>

它给了我以下错误:

解析错误:语法错误,意外的$ end在C:\ xampp \ htdocs \ eclipseWorkspaceWebDev \ MandatoryHandinSite \ tester.php(4):eval()'代码在第1行

有人知道这个问题的解决方案。

php math eval
9个回答
43
投票

虽然我不建议使用eval(这不是解决方案),但问题是eval需要完整的代码行,而不仅仅是片段。

$ma ="2+10";
$p = eval('return '.$ma.';');
print $p;

应该做你想做的。


更好的解决方案是为数学表达式编写tokenizer / parser。这是一个非常简单的基于正则表达式的例子,给你一个例子:

$ma = "2+10";

if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $ma, $matches) !== FALSE){
    $operator = $matches[2];

    switch($operator){
        case '+':
            $p = $matches[1] + $matches[3];
            break;
        case '-':
            $p = $matches[1] - $matches[3];
            break;
        case '*':
            $p = $matches[1] * $matches[3];
            break;
        case '/':
            $p = $matches[1] / $matches[3];
            break;
    }

    echo $p;
}

28
投票

看看这个..

我在会计系统中使用它,你可以在数量输入字段中编写数学表达式。

Examples

$Cal = new Field_calculate();

$result = $Cal->calculate('5+7'); // 12
$result = $Cal->calculate('(5+9)*5'); // 70
$result = $Cal->calculate('(10.2+0.5*(2-0.4))*2+(2.1*4)'); // 30.4

Code

class Field_calculate {
    const PATTERN = '/(?:\-?\d+(?:\.?\d+)?[\+\-\*\/])+\-?\d+(?:\.?\d+)?/';

    const PARENTHESIS_DEPTH = 10;

    public function calculate($input){
        if(strpos($input, '+') != null || strpos($input, '-') != null || strpos($input, '/') != null || strpos($input, '*') != null){
            //  Remove white spaces and invalid math chars
            $input = str_replace(',', '.', $input);
            $input = preg_replace('[^0-9\.\+\-\*\/\(\)]', '', $input);

            //  Calculate each of the parenthesis from the top
            $i = 0;
            while(strpos($input, '(') || strpos($input, ')')){
                $input = preg_replace_callback('/\(([^\(\)]+)\)/', 'self::callback', $input);

                $i++;
                if($i > self::PARENTHESIS_DEPTH){
                    break;
                }
            }

            //  Calculate the result
            if(preg_match(self::PATTERN, $input, $match)){
                return $this->compute($match[0]);
            }
            // To handle the special case of expressions surrounded by global parenthesis like "(1+1)"
            if(is_numeric($input)){
                return $input;
            }

            return 0;
        }

        return $input;
    }

    private function compute($input){
        $compute = create_function('', 'return '.$input.';');

        return 0 + $compute();
    }

    private function callback($input){
        if(is_numeric($input[1])){
            return $input[1];
        }
        elseif(preg_match(self::PATTERN, $input[1], $match)){
            return $this->compute($match[0]);
        }

        return 0;
    }
}

1
投票

当你无法控制字符串参数时,使用eval函数是非常危险的。

尝试使用Matex进行安全的数学公式计算。


1
投票

我最近创建了一个PHP包,它提供了math_eval辅助函数。它完全符合您的需要,无需使用潜在的不安全的eval功能。

您只需传入数学表达式的字符串版本,然后返回结果。

$two   = math_eval('1 + 1');
$three = math_eval('5 - 2');
$ten   = math_eval('2 * 5');
$four  = math_eval('8 / 2');

您还可以传入变量,如果需要,将替换这些变量。

$ten     = math_eval('a + b', ['a' => 7, 'b' => 3]);
$fifteen = math_eval('x * y', ['x' => 3, 'y' => 5]);

链接:https://github.com/langleyfoxall/math_eval


0
投票

eval将给定的代码评估为PHP。这意味着它将作为PHP代码执行给定的参数。

要更正您的代码,请使用以下命令:

$ma ="print (2+10);";
eval($ma);

0
投票

解决了!

<?php 
function evalmath($equation)
{
    $result = 0;
    // sanitize imput
    $equation = preg_replace("/[^a-z0-9+\-.*\/()%]/","",$equation);
    // convert alphabet to $variabel 
    $equation = preg_replace("/([a-z])+/i", "\$$0", $equation); 
    // convert percentages to decimal
    $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
    $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
    $equation = preg_replace("/([0-9]{1})(%)/",".0\$1",$equation);
    $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation);
    if ( $equation != "" ){
        $result = @eval("return " . $equation . ";" );
    }
    if ($result == null) {
        throw new Exception("Unable to calculate equation");
    }
    echo $result;
   // return $equation;
}


$a = 2;
$b = 3;
$c = 5;
$f1 = "a*b+c";

$f1 = str_replace("a", $a, $f1);
$f1 = str_replace("b", $b, $f1);
$f1 = str_replace("c", $c, $f1);

evalmath($f1);
/*if ( $equation != "" ){

    $result = @eval("return " . $equation . ";" );
}
if ($result == null) {

    throw new Exception("Unable to calculate equation");
}
echo $result;*/
?>

0
投票

这种方法有两个主要缺点:

  • 安全性,php脚本正由eval函数评估。这很糟糕,特别是当用户想要注入恶意代码时。
  • 复杂

我创建了这个,看看:Formula Interpreter

How does it work ?

首先,使用公式及其参数创建FormulaInterpreter的实例

$formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]);

使用execute()方法解释公式。它将返回结果:

echo $formulaInterpreter->execute();

在一条线上

echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute();

Examples

# Formula: speed = distance / time
$speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ;
echo $speed;


#Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day
$parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8];
$venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute();
echo $venezuelaLOTTTArt118NightOvertime;


#cicle area
$cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute();
echo $cicleArea;

About the formulas

  1. 它必须包含至少两个操作数和一个运算符。
  2. 操作数的名称可以是大写或小写。
  3. 到目前为止,不包括数学函数sin,cos,pow ......我正在努力将它们包括在内。
  4. 如果您的公式无效,您将收到如下错误消息:错误,您的公式(single_variable)无效。
  5. 参数的值必须为数字。

You can improve it if you want to!


0
投票

使用eval函数

 protected function getStringArthmeticOperation($value, $deduct)
{
    if($value > 0){
        $operator = '-';
    }else{
        $operator = '+';
    }
    $mathStr = '$value $operator $deduct';
    eval("\$mathStr = \"$mathStr\";");
    $userAvailableUl = eval('return '.$mathStr.';');
    return $userAvailableUl;
}

$this->getStringArthmeticOperation(3, 1); //2

-2
投票

eval'd表达式应以“;”结尾

试试这个 :

$ma ="2+10;";
$p = eval($ma);
print $p;

顺便说一下,这超出了范围,但'eval'函数不会返回表达式的值。 eval('2 + 10')不会返回12.如果你想让它返回12,你应该eval('return 2 + 10;');

© www.soinside.com 2019 - 2024. All rights reserved.