用于评估match(),组合之间的Eval表达式

时间:2015-07-17 06:46:21

标签: php eval logical-operators

我需要为like,between,contains和match()表达php eval。 我已经为其他逻辑运算符做了很好的工作。

E.g。

echo eval("return ('1'==1&&'en-au'!='en-us');");

但我必须评估这个表达式:

echo eval("return ('xxxx.match(xxx) != null');");

总是回归真实。有人可以解释如何评估这种表达方式。

1 个答案:

答案 0 :(得分:1)

对于初学者,以下是如何评估preg_match()

的示例
$test = "abFc";
echo eval("return preg_match('#F#', '$test');");

这将输出:1

语法为'xxxx.match(xxx) != null'不是有效的PHP语法,所以你不能直接使用eval() - 它需要是有效的PHP。在您的javascript示例中,xxxxxxx是变量。他们需要重写,以便他们可以在PHP中进行评估。在我的示例中,我已将语句重写为eval("return (match('$xxxx', '$xxx') !== null);")

您可以编写一个函数,从表中传递变量,并根据需要进行尽可能多的比较和代码行进行评估。

例如:

$xxxx = "abFc";
$xxx = "F";

$eval = eval("return (match('$xxxx', '$xxx') !== null);");
if ($eval) {
    echo "true";
} else {
    echo "false";
}


function match($string, $regex){
    // match: executes a search for a match in a string. It returns an array of information or null on a mismatch.    

    preg_match_all('#' . $regex . '#', $string, $amatch);
    if(count($amatch[0]) > 0){
        return $amatch[0];
    } else {
        return null;
    }   
}

另外,请阅读有关使用eval()的警告您需要确保传递给它的数据是安全的。

相关问题