iwarlord Posted December 8, 2008 Share Posted December 8, 2008 Does anyone know of a string to sum type function? I'm wanting to convert a string such as "22-12-6+5" into an int 9. I can't find anything in the documentation, but maybe I'm missing something. Can anyone help? Thanks. Link to comment https://forums.phpfreaks.com/topic/136021-string-to-sum/ Share on other sites More sharing options...
.josh Posted December 8, 2008 Share Posted December 8, 2008 Make sure you validate $string if you're getting that from user (like from a form). $string = "22-12-6+5"; eval("\$sum = $string;"); echo $sum; Link to comment https://forums.phpfreaks.com/topic/136021-string-to-sum/#findComment-709213 Share on other sites More sharing options...
corbin Posted December 8, 2008 Share Posted December 8, 2008 http://www.google.com/search?hl=en&q=php+math+parsing&btnG=Google+Search&aq=f&oq= If you didn't want to have to use eval, you could always write a parser with regular expressions. A small parser that can just handle the basic operations wouldn't be hard at all. But, as long as that is the case, eval would work just as well, be faster, and of course be easier. Link to comment https://forums.phpfreaks.com/topic/136021-string-to-sum/#findComment-709931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.