The most secure and easy to maintain option will be to program a dedicated "mini-language" in php. You can make it a subset of php, or make it look like Excel formulas, or even invent your own one. This way you'll always have the full control of what's happening.
//
just for fun, here's a small Lisp for you
function lisp($x) {
if(is_string($x)) {
$re = '~\(([^()]*)\)~';
while(preg_match($re, $x))
$x = preg_replace_callback($re, 'lisp', $x);
return trim($x);
}
$x = preg_split('~\s+~', $x[1]);
$e = array_shift($x);
if(!$x)
return is_numeric($e) ? floatval($e) : $e;
switch($e) {
case '+': return lisp($x[0]) + lisp($x[1]);
case '-': return lisp($x[0]) - lisp($x[1]);
case '*': return lisp($x[0]) * lisp($x[1]);
case '/': return lisp($x[0]) / lisp($x[1]);
case 'concat': return lisp($x[0]) . lisp($x[1]);
}
return function_exists($e) ?
call_user_func_array($e, array_map('lisp', $x)) : '';
}
$input = '
(strtolower
(concat
(strrev olleh)
(+ 22 20)))';
echo lisp($input); // hello42
;))