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
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • possible duplicate of [How to evaluate formula passed as string in PHP?](http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php) – Qarib Haider Jul 21 '14 at 10:41
  • no! what I mean is different than How to evaluate formula passed as string in PHP? – user3472449 Jul 21 '14 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 .. – Qarib Haider Jul 21 '14 at 10:48
  • http://stackoverflow.com/questions/5057320/php-function-to-evaluate-string-like-2-1-as-arithmetic-2-1-1 – Steve Jul 21 '14 at 11:06

1 Answers1

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);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Your expression must contain a `return` statement, otherwise eval won't return the results of the expression. I've added a small example. – GolezTrol Jul 21 '14 at 10:46
  • 1
    eval requires cleanup to prevent execution of malicious code .. thats why its best to avoid this .. – Qarib Haider Jul 21 '14 at 10:46
  • @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? – GolezTrol Jul 21 '14 at 10:48
  • 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 Jul 22 '14 at 08:21
  • @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. – GolezTrol Jul 22 '14 at 08:30
  • For a specific answer, `eval` is absolutely horrible for solving math equations in PHP, especially when so many libraries exist that can do it completely safely. (And no need answering when I believe it to be a duplicate of a question I've already answered that someone else already linked to ^^) – Jon Jul 22 '14 at 08:32