php - Calculate numbers from string -


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

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -