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

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -