从字符串计算数字

时间:2017-04-07 18:45:36

标签: php

我正在尝试计算字符串中的数字 示例:

<?php
$str = '6 + 12 - ( 2 * 3 )';
$results = some_function($str);
Echo $results;
// outputs 12 
?>

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

您可以通过eval函数完成此操作(它运行一个字符串,就像它是PHP一样)。

<?php

function some_function($str) {
    eval("return " . $str . ";");
}

$str = '6 + 12 - ( 2 * 3 )';
$results = some_function($str);
echo $results;


?>
相关问题