No! Just don't!
If you are scraping data anywhere it's not under your direct control, never ever, no - really! not ever! - eval it. And if the data is under your control and you need to eval it, you are doing things in a very very wrong way. Most likely anyway.
What happens the day
4 + 5 = 9
is replaced with
'seriously harmful shell commands or nasty php code';
If you need to calculate a math problem described as a string, you will need to parse the string before solving the problem. The best way I can think of is continously separating each expression into its left and right hand sides, since these will be either an expression or an atomic value. When you reach an expression with atomic values on both sides, you calculate the result of that expression by performing a check on the operator
function calc($left, $right, $op)
{
switch ($op)
{
case '+':
return $left + $right;
case '-':
return $left - $right;
# etc ...
}
}
And obviously, to handle more complex expressions, you must deal with parentheses and operator precedence so that you don't end up replacing
2 + 3 * 4
with
calc(calc(2, 3, '+'), 4, '*');
but rather
calc(2, calc(3, 4, '*'), '+');