It just trying, but maybe good start.
$somm = 0;
$a = 30;
$str = "(a x 5% - 2%)";
$pt1 = "/x/i";
$str = preg_replace($pt1, "*", $str);
$pt2 = "/([a-z])+/i";
$str = preg_replace($pt2, "\$$0", $str);
$pt3 = "/([0-9])+%/";
$str = preg_replace($pt3, "($0/100)", $str);
$pt4 = "/%/";
$str = preg_replace($pt4, "", $str);
$e = "\$comm = $str;";
eval($e);
echo $e . "
";
echo $comm;