Php 字符串是数学表达式吗?

Php 字符串是数学表达式吗?,php,math,Php,Math,如何确定字符串是否为数学表达式 理解基本的数学表达式+、-、x、/ 例如: "1+1" => TRUE "2 / 2" => TRUE "hello" => FALSE "1 * 2 - X" => FALSE "me + u" => FALSE 可能是一个具有如下模式的正则表达式: ^([-+/*]\d+(\.\d+)*一个非常简单的解决方案: 正则表达式编号,空格,[+,/,*,-,=],空格,子字符串(此处递归) 将适用于任何序列的 1 + 1 +

如何确定字符串是否为数学表达式

理解基本的数学表达式+、-、x、/

例如:

"1+1" => TRUE

"2 / 2" => TRUE

"hello" => FALSE

"1 * 2 - X" => FALSE

"me + u" => FALSE

可能是一个具有如下模式的正则表达式:


^([-+/*]\d+(\.\d+)*
一个非常简单的解决方案: 正则表达式编号,空格,[+,/,*,-,=],空格,子字符串(此处递归) 将适用于任何序列的 1 + 1 + 2 + ... + 1 = 2 + 3 + 4 = 1 * 4 ... 等等

显然不会检查表达式是否合法

根据要求,伪代码:

if  Regex(^(([0-9]+)(\s*)([+,/,*,-,=])(\s*)([0-9]+)(\s*)([+,/,*,-,=])(\s*)))
    if (recursion())
         return True;
    else
         return False;
else //checking for end point
    if Regex(^(([0-9]+)(\s*)([+,/,*,-,=])(\s*)([0-9]+)))
         return True;
    else
         return False;

了解更多信息

Define:math expression!怎么样
1*2-X
me+u去就餐/餐厅
?检查此
me+u去就餐/账单/2
@Ejay;-)这只是一个很好的回答@Serj,你能提供实际的正则表达式吗?这不是为了我的享受,而是为了答案的质量等等。为你的努力收回那张否决票。哇!来自:“eval()语言构造非常危险,因为它允许执行任意PHP代码。因此不鼓励使用它。如果您已经仔细验证了除了使用此构造之外没有其他选择,请特别注意,在没有事先正确验证的情况下,不要将任何用户提供的数据传递给它。”@esqew是的,这就是为什么我把它放在第二个选项上。你可以选择第一个:)
class MathExpression {

    private static $parentheses_open = array('(', '{', '[');
    private static $parentheses_close = array(')', '}', ']');

    protected static function getParenthesesType( $c ) {
        if(in_array($c,MathExpression::$parentheses_open)) {
            return array_search($c, MathExpression::$parentheses_open);
        } elseif(in_array($c,MathExpression::$parentheses_close)) {
            return array_search($c, MathExpression::$parentheses_close);
        } else {
            return false;
        }
    }

    public static function validate( $expression ) {
        $size = strlen( $expression );
        $tmp = array();
        for ($i=0; $i<$size; $i++) {
            if(in_array($expression[$i],MathExpression::$parentheses_open)) {
                $tmp[] = $expression[$i];
            } elseif(in_array($expression[$i],MathExpression::$parentheses_close)) {
                if (count($tmp) == 0 ) {
                    return false;
                }
                if(MathExpression::getParenthesesType(array_pop($tmp)) 
                    != MathExpression::getParenthesesType($expression[$i])) {
                    return false;
                }
            }
        }
        if (count($tmp) == 0 ) {
            return true;
        } else {
            return false;
        }
    }
}

//Mathematical expressions to validate
$tests = array(
    '(A1+A2*A3)+A5+(B3^B5)*(C1*((A3/C2)+(B2+C1)))',
    '(A1+A2*A3)+A5)*C1+(B3^B5*(C1*((A3/C2)+(B2+C1)))',
    '(A1+A2*A3)+A5++(B2+C1)))',
    '(A1+A2*A3)+A5+(B3^B5)*(C1*(A3/C2)+(B2+C1))'
);

// running the tests...
foreach($tests as $test) {
    $isValid = MathExpression::validate( $test );
    echo 'test of: '. $test .'<br>';
    var_dump($isValid);
}
$result = INF;
try { 
  eval("$result=" + myMathExpression);  // Evaluate here
} catch (Exception $e) { 

} 
if($result != INF) echo("Expression is a valid mathematical expression.");