4

I have this string for an instance:

"2 + 2 - 2" and when evaluated it should return int(2);

I'm looking for a function/parser witch can determine if anykind of math content is matched within a string. More examples:

"(2 + 2) / 2"
"(4 / 8) * 12"
"128 * 8"

Do not need to evaluate and calculate these Math expressions, just need a function witch will determine (True/False return values), if the statement is of this Math kind.

Is this possible with regex or something? Thanks!

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360

4 Answers4

1

Maybe run it as php on runtime and check that the responding value comes back as numeric. Take a look at this. http://php.net/manual/en/function.eval.php

Luc
  • 985
  • 7
  • 10
  • 2
    That sounds like a bad idea, given that you can execute arbitrary PHP this way. – Oliver Charlesworth Mar 22 '12 at 14:49
  • 1
    yes, it's a little insecure, but with some regex wrapped around your input to test that they are entering numbers and only certain symbols it could be the best way to get a sum back. might be handy also for extending into better math functions if that eventually becomes a requirement. – Luc Mar 23 '12 at 02:35
0

See eval. For example, you can do this:

$result = INF;
try { 
  eval("$result=" + myMathExpression);  // Evaluate here
} catch (Exception $e) { 

} 
if($result != INF) echo("Expression is a valid mathematical expression.");

Just be careful, because any PHP code will be evaluated!

Diego
  • 18,035
  • 5
  • 62
  • 66
  • 2
    I guess he wants to evaluate if someone typed in a proper math expression. putting that straight into `eval()` is suicide. – devsnd Mar 22 '12 at 15:00
  • 1
    I've used your example and it works. I know I'm leaving a security hole here regardings the "eval" function, but I'll take care about that later. :) Thank you. –  Mar 22 '12 at 15:02
  • 1
    "Taking care later" is never a good option. I still recommend using some kind of regular expression, like `/^[0-9\+\-\*\/()]+$/`, that checks if there are only valid characters. Later, you can then refine that, instead of leaving a gaping hole behind right now. – devsnd Mar 22 '12 at 15:05
  • I'm writing a simple custom-code parser just to get an idea how it might work. This is my project just for testing these kinds of eniviroments and stuff. :) –  Mar 22 '12 at 15:07
  • 1
    Alright: If it doesn't get out in the open I won't complain. I just want to make sure, that everybody who sees this question is aware of the security implications of `evil()` ... errr ... `eval()` – devsnd Mar 22 '12 at 15:12
  • Yes. I'm aware of that as well. :) My current knowledge about creating programming/scripting languages is not high, so if you have any other links or articles to recommend on this matter, please do so @twall. :) thanks. –  Mar 22 '12 at 15:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9196/discussion-between-commando-and-twall) –  Mar 22 '12 at 15:19
0

If you know what type of expressions you will be parsing you could try to use regular expressions. Something like ^\d+\s*(\+|-|\*|\\)\s*\d+ should yield true if you are trying to validate the binary operators of +,*, - and \. So it would validate items such as 3 + 3, 2 - 0, etc.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

You can use a parser generator from XP-framework. See sample grammar for calculator, which can be translated to PHP code.

Ali
  • 206
  • 1
  • 4