首页 > 代码库 > php字符串算术表达式计算

php字符串算术表达式计算

 

$aa = "{1}*{2}-{3}";
$farr = array(‘/\{1\}/‘,‘/\{2\}/‘,‘/\{3\}/‘);
$tarr = array(3,4,10);
$str = preg_replace( $farr,$tarr,$aa);
echo $str;        //结果:3*4-10
echo eval(‘return ‘.$str.‘;‘);   //结果:2

来源:http://blog.51yip.com/php/1846.html

php字符串算术表达式计算