mysterbx Posted February 19, 2008 Share Posted February 19, 2008 How can i make this result: "12-1" to output like this: "11" ? In other words: I get this result from my script: 12-1 I need to make the script do the match: 12-1=11, so the $result would be 11 Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 19, 2008 Share Posted February 19, 2008 If I understand properly, you are retrieving the string value of "12-1" and you need to execute that equation rather than displaying it to the screen. If this is the case, you will either need to build a parser of some sort (safer) or use eval() to execute the equation (easier but more dangerous): <?php $eq = "12-1"; eval("\$result = $eq;"); echo $result; ?> Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 19, 2008 Author Share Posted February 19, 2008 yeap that works great! Quote Link to comment Share on other sites More sharing options...
btherl Posted February 20, 2008 Share Posted February 20, 2008 obsidian hinted at this, but don't put that code out for public use! It will allow your users to execute any command they want on your webserver. You can avoid this by sanitizing the input, for example: $safe_eq = preg_replace('|[^[:digit:]()+*/-]|', '', $eq); That will allow only digits and the following symbols: ( ) + * / - I think that ought to be safe.. at least I can't imagine anything you could do with that other than causing the eval to fail due to syntax error. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 20, 2008 Author Share Posted February 20, 2008 code is for timed backups... It gets the time, then gets the timezone. And the output is: 15(time)-5(timezone), in otherwords: 15-5 and I needed a code that "does the math" the command runs in background, so no publick use will be allowed... Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 20, 2008 Share Posted February 20, 2008 code is for timed backups... It gets the time, then gets the timezone. And the output is: 15(time)-5(timezone), in otherwords: 15-5 and I needed a code that "does the math" the command runs in background, so no publick use will be allowed... If that's the case, I would recommend you do something like this (I'm one to always go above and beyond on precautions): <?php // since you know it will always be [digits] + or - [digits], this will work: function parseTime($string) { if (!preg_match('|^([\d]+)(+|-)([\d]+)$|', $string, $match)) { // Invalid format presented return FALSE; } switch ($matches[2]) { case '+': return intval($matches[1]) + intval($matches[3]); break; case '-': return intval($matches[1]) - intval($matches[3]); break; } } ?> Hope this makes sense. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 21, 2008 Author Share Posted February 21, 2008 that works... 10x! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.