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.

3
  • 3
    There's always eval()... I don't dare post it as an answer - I'm sure it will invite flames. Commented Jun 14, 2011 at 6:34
  • 1
    @Wesley Murch Let the flames begin! Commented Jun 14, 2011 at 6:37
  • similar : stackoverflow.com/questions/1015242/… Commented Jun 14, 2011 at 6:58

3 Answers 3

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  
Sign up to request clarification or add additional context in comments.

9 Comments

@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 Thanks Tom - I was about to spend the rest of the day to make it bullet proof...
@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 Munch Maybe you/i should open a question specifically for this itch... I have some crazy ideas...
@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?
|
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 . + - * / ( )

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.