0

I want to evaluate a mathematical operations inside the string after I get it in that string. Here is the string

$string = "Add this two numbers [4+2+2]. And [5*3/2] will result to?" 

I already get those numbers:

$f_number = "4+2+2"; $s_number = "5*3/2"; 

How can I evaluate this automatically using any function? Sample:

echo anyfunction($f_number);//will result to 8 echo anyfunction($s_number);//will result to 7.5 

because if I will echo directly it will just output like this:

echo $f_number;//will result to 4+2+2 echo a$s_number;//will result to 5*3/2 
4
  • possible duplicate of How to evaluate formula passed as string in PHP? Commented Jul 21, 2014 at 10:41
  • no! what I mean is different than How to evaluate formula passed as string in PHP? Commented Jul 21, 2014 at 10:45
  • from your question what I understood is 'you want to execute mathematic operation passed as string' .. then thats what is answered in that question .. Commented Jul 21, 2014 at 10:48
  • stackoverflow.com/questions/5057320/… Commented Jul 21, 2014 at 11:06

1 Answer 1

0

You can use eval. It's probably the easiest way out. Mind though that it can also be used for other expressions, because it basically executes any PHP code that is in the string.

But by wrapping it in a function, like you intended, you can at least black box it, and add safety measures later if you need to, or even switch to a different expression evaluator, without having to change all your code.

A simple safety measure would be to check if the string only contains numeric values, whitespace and allowed operators. That way it should be impossible to secretly inject actual code.

function anyfunction($expr) { // Optional: check if $expr contains only numerics and operators // actual evaluation. The code in $expre should contain a return // statement if you want it to return something. return eval("return $expr;"); } echo anyfunction($f_number); 
Sign up to request clarification or add additional context in comments.

6 Comments

Your expression must contain a return statement, otherwise eval won't return the results of the expression. I've added a small example.
eval requires cleanup to prevent execution of malicious code .. thats why its best to avoid this ..
@SyedQarib Thanks for your generic comment. Whether you 'require' it, also depends on the situation. But nevertheless I mentioned that you should, even in the comments in the code snippet. Did you read my answer?
If you're going to direct someone to eval, even saying it needs to be cleaned up, you should show a basic regex to go with it to make it safe. Just saying it could (optionally) be checked is negligent.
@Jon Thanks for your feedback. For me, I like to answer the question to the point, and I think the warning is enough. It is an answer for a specific issue, not a complete tutorial. Calling me negligent even though I gave the warning, and pointed to the documentation page which repeats that warning, isn't fair. I just assume OP isn't a retard, so they will probably take my warning seriously.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.