最安全和易于维护的选项是用PHP编写专用的“迷你语言”。你可以让它成为PHP的一个子集,或者让它看起来像Excel公式,甚至可以自己发明一个。这样你就可以完全控制正在发生的事情。
/ /
只是为了好玩,这里有一个小口齿不清给你
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
;)