4

I need to have a text input field do or not do basic calculations. Example

<input type="text" />

I then submit by post and the post will know to either enter the value or do calculations

Value examples

14.5
10.3+14+35

Should give me respectively

14.5
59.3

Does anyone know of a method, script,...anything to do this? Or any idea on how to go about it?

Thanks.

denislexic
  • 10,786
  • 23
  • 84
  • 128

3 Answers3

4

Without using eval but you still need to be careful and catch any parsing errors, for example trying to evaluate ')' etc. I have ideas but will leave them for the reader or another rainy day.

reference site

function calculate_string( $mathString ) {

    // trim white spaces
    $mathString = trim($mathString);

    // remove any non-numbers chars; exception for math operators
    $mathString = ereg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString);

    $compute = create_function("", "return (" . $mathString . ");" );
    return 0 + $compute();

}

$string = " (1 + 1) * (2 + 2)";
echo calculate_string($string);  // outputs 8  
zaf
  • 22,776
  • 12
  • 65
  • 95
  • @Wesley: "open to errors" is not the same as "security hole". @zaf's answer is a solution to the question... the question didn't ask about guaranteeing a valid syntax tree. – Tom Jun 14 '11 at 06:54
  • @Tom Thanks Tom - I was about to spend the rest of the day to make it bullet proof... – zaf Jun 14 '11 at 06:56
  • @Tom: Even though the question didn't ask, we really should be poitning this stuff out when giving advice, as zaf has considerately chosen to do. Aside, I feel like there really *must* be a legit way to do this. – Wesley Murch Jun 14 '11 at 07:00
  • @Wesley Munch Maybe you/i should open a question specifically for this itch... I have some crazy ideas... – zaf Jun 14 '11 at 07:03
  • @Wesley: what would constitute a correct solution for you? If there's a syntax error in the input calculation, this answer gives an error, which could be directed back to the user using an exception handler. What else should a 'PHP calculator' do? – Tom Jun 14 '11 at 07:04
  • @Tom: To my knowledge, parse errors (which is what will occur here and with the eval approach) will stop the script dead in it's tracks even if you try to use an exception handler. Am I mistaken? – Wesley Murch Jun 14 '11 at 10:06
  • @Wesley: No, having looked at it, you're right: PHP parse errors cannot have custom handlers. So the only way to return a (calculation string) parse error to the user gracefully would be to actually write a parser. – Tom Jun 14 '11 at 10:28
  • Thanks for the effort guys! Much appreciated. – denislexic Jun 16 '11 at 13:10
  • And don't forget to add the "." in your regex so you can compute with float (like 0.1 + 0.2 = 0.3) – Fabien Sa Jan 16 '13 at 09:52
0

With the following regular expression you can check whether the input is valid mathematical syntax and use eval():

/^(\(*[\+\-]?\(*[\+\-]?\d+(.\d+)?\)*[\+\-\*\/]?)*$/

Allowed characters are: 1 2 3 4 5 6 7 8 9 0 . + - * / ( )

-1

One quick solution would be to check with substr() if the input has any math operators (+, -, ...) in it. If so, do the math either using eval() (quite a dirty hack, since it accepts all PHP code and not only math) or build the appropriate tree of operations manually and then produce the results. If there are no math operators, just print out the input.

deltaforce2
  • 583
  • 2
  • 8