Reputation: 509
I need to do php eval for expression for like, between, contains and match()
.
I have done for other logical operators and its working fine.
E.g.
echo eval("return ('1'==1&&'en-au'!='en-us');");
But i have to evaluate this expression:
echo eval("return ('xxxx.match(xxx) != null');");
Which always return true. Can someone explain how to eval this type of expressions.
Upvotes: 1
Views: 439
Reputation: 5670
For starters, here is an example of how to evaluate preg_match()
$test = "abFc";
echo eval("return preg_match('#F#', '$test');");
This will output: 1
The syntax be 'xxxx.match(xxx) != null'
is not valid php syntax and so you cannot use eval()
on it directly - it needs to be valid php. In your javascript example, xxxx
and xxx
are variables. They need to be rewritten so they may evaluate in php. In my example, I've rewritten the statement to eval("return (match('$xxxx', '$xxx') !== null);")
You can write a function, pass the variable from the table and do whatever evaluation you need to do with as many comparisons and lines of code as it takes.
For example:
$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;
}
}
Also, read the warnings regarding using eval()
You need to make sure the data you pass to it is known to be safe.
Upvotes: 1