php - Calculate numbers from string -
this question has answer here:
- how mathematically evaluate string “2-1” produce “1”? 7 answers
- how make calculator in php? 3 answers
i'm try calculate numbers inside string example:
<?php $str = '6 + 12 - ( 2 * 3 )'; $results = some_function($str); echo $results; // outputs 12 ?> can me this?
you can accomplish via eval function (it runs string if php).
<?php function some_function($str) { eval("return " . $str . ";"); } $str = '6 + 12 - ( 2 * 3 )'; $results = some_function($str); echo $results; ?>
Comments
Post a Comment