Execute a validation inserted in a string

Asked

Viewed 30 times

0

I have a variable in PHP that mounts a logical string.

$str = "(('TR1'=='TR' OR 'TR2=='TR' OR 'TR'=='TR') AND ('10,0'=='10,1' OR '9,0'=='9,0'))";

I’d like to find a way that validation is possible, like:

if($str) "One of the parameters on each side is valid";

Has anyone been through it, or knows a way to solve it?

  • These parameters TR1, TR, TR2, etc are strings, in which case the first part will always be true since 'TR' == 'TR' is true. In the second part, after the AND, it will also always be true, because '9.0' == '9.0'. In this case it doesn’t make much sense why the IF. You can explain better?

  • Of course, David, first of all, thank you so much for your interest in helping.. In the example yes the validation would always be true, but the real case is that the string will be the assembly of a reading in a database, where the expression itself is stored. So I need to validate and only proceed when the result of this expression is true.

1 answer

2


To run a String as a command in PHP, you can use the "Eval()".

<?php

eval('$var = (("TR1"=="TR" OR "TR2"=="TR" OR "TR"=="TR") AND ("10,0"=="10,1" OR "9,0"=="9,0"));');

echo $var;

?>

http://php.net/manual/en/function.eval.php

  • Ederson, thank you very much, I had even tried to use the function Eval(), but had not succeeded. Very grateful hand skin.. in this way worked out...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.