I don't think ^ in this context is the bitwise xor operator...
I'd go along with MarkR on this one: you'll want to write yourself a parser (in no small part because eval() is dangerous). But hey, it's a core exercise in programming ability, so it's worth trying. I'd be tempted to be lazy about it and use regexps to do the lifting and carrying:
evaluate_parens($paren_expression)
{
return evaluate($paren_expression[1]);
}
evaluate_exponent($exponentiation)
{
return pow($exponentiation[1],$exponentiation[2]);
}
evaluate($expression,$substitutions=null)
{
// Subsitutions is an array('varname'=>value, ...)
if(!is_null($substitutions)
$expression = str_replace(array_keys($substitutions), array_values($substitutions), $expression);
$expression = preg_replace_callback('/\(([^)]+)\)/', 'evaluate_parens', $expression);
$expression = preg_replace_callback('/(\d+)^(\d+)/', 'evaluate_exponent', $expression);
//...
return $expression;
}
That's a sketch of one off-the-cuff idea for an approach, anyway.