Jump to content

String to equation


bundyxc

Recommended Posts

Let's say that I have a code that gets form input in the form of an equation as a string. How would I make my program do this equation?

 

<?php
$input = '2 + 2'; //$input is a string
$result = 2 + 2; //I'm not sure how to take the string and put it into equation form.
print($result);

 

 

Link to comment
https://forums.phpfreaks.com/topic/174395-string-to-equation/
Share on other sites

Please NOT eval(). OP has used $input suggesting that this comes from user input, so eval() would definitely be unsafe.

Such formulae need parsing. I'd recommend PHPExcel's calculation engine, but that may well be overkill; but I'm aware of at least one halfway decent formula parser in PHPClasses that would allow formulae typed in by a user to be safely parsed and evaluated.

Link to comment
https://forums.phpfreaks.com/topic/174395-string-to-equation/#findComment-919437
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.