bundyxc Posted September 16, 2009 Share Posted September 16, 2009 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 More sharing options...
Daniel0 Posted September 16, 2009 Share Posted September 16, 2009 That's not an equation. Just saying. Link to comment https://forums.phpfreaks.com/topic/174395-string-to-equation/#findComment-919342 Share on other sites More sharing options...
Zane Posted September 16, 2009 Share Posted September 16, 2009 eval Link to comment https://forums.phpfreaks.com/topic/174395-string-to-equation/#findComment-919345 Share on other sites More sharing options...
Mark Baker Posted September 16, 2009 Share Posted September 16, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.