I need to parse some expressions stored in strings, e.g.:
$variables = array(
'variable1' => 3,
'variable2' => 4,
'variable3' => 'test string',
'variable4' => false,
'variable5' => true,
);
$exp1 = "2+2";
$exp2 = "variable1 * 5 + variable2";
$exp3 = " variable3.' concat string' ";
$exp4 = " variable4 || variable5 ";
$result1 = ExpressionParser($exp1); // 4
$result2 = ExpressionParser($exp2); // 19
$result3 = ExpressionParser($exp3); // "test string concat string"
$result4 = ExpressionParser($exp4); // true
So I need to write ExpressionParser() function...
Syntax of expressions is differs from PHP expressions only by variables definition - there is no $ sign before variables name.
All variables values is stored in $variables array.
I thinkin about using of eval() function (adding a $ signs in expression variables first) but this function will die with PHP Parsing Error if syntax error occured in expression and it insecure. It executes PHP code stored in variable and this is little differs from what I need.
And I pretty don't want to write my own expression parser completely - using some php functions is more preferable.
Any thoughts?