0

I need to convert the formula like string to its calculated value, in PHP.

For eg:

$str = "(20000+(2000*(40/100)))*(10/100)";
$calculated_value = ????; // Calculated value of the above string "$str".

I need to calculate value of the string "$str" and store it in separate string. Please help me out.

Thanks in advance.

Raidri
  • 17,258
  • 9
  • 62
  • 65
Ashok
  • 128
  • 1
  • 8
  • If someone suggests using a function starting with an `e` and ending on `al`, then ignore that. You need to parse this string (possibly using a stack-based approach). Fun! – Lekensteyn Mar 14 '13 at 09:29
  • Take a look at @ircmaxell's answer to this previous version of your question: http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php - it's the best PHP answer you'll find – Mark Baker Mar 14 '13 at 09:38

1 Answers1

1

Take a look on this: http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html

And Process mathematical equations in php

Community
  • 1
  • 1
user1236048
  • 5,542
  • 7
  • 50
  • 87