note 97681 added to function.eval

From: Date: Sun, 02 May 2010 22:22:37 +0000
Subject: note 97681 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-168865@lists.php.net to get a copy of this message
This is a secure matheval that can handle maths functions as well. Complements cmr at expansys dot com matheval. <?php function matheval($string) { $function = array( "abs", "acos", "acosh", "asin", "asinh", "atan2", "atan", "atanh", "base_convert", "bindec", "ceil", "cos", "cosh", "decbin", "dechex", "decoct", "deg2rad", "exp", "expm1", "floor", "fmod", "getrandmax", "hexdec", "hypot", "is_finite", "is_infinite", "is_nan", "lcg_value", "log10", "log1p", "log", "max", "min", "mt_getrandmax", "mt_rand", "mt_srand", "octdec", "pi", "pow", "rad2deg", "rand", "round", "sin", "sinh", "sqrt", "srand", "tan", "tanh", ); $chars = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '(', ')', '/', '*', '-', '+', ); for ($i = 0; $i < strlen($string); $i++) { if (in_array($string[$i], $chars)) continue; $fct = $string[$i]; while (in_array_partial(($fct . $string[$i + 1]), $function)) $fct .= $string[++$i]; if (!in_array($fct, $function)) return ("SECURITY ERROR"); } return (eval("echo " . $string . ";")); } function in_array_partial($string, Array $array) { foreach ($array as $value) if (!strncmp($string, $value, strlen($string))) return (true); return (false); } ?> ---- Server IP: 195.221.21.36 Probable Submitter: 82.228.40.72 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/97681 Del: integrated -- https://master.php.net/note/delete/97681/integrated Del: useless -- https://master.php.net/note/delete/97681/useless Del: bad code -- https://master.php.net/note/delete/97681/bad+code Del: spam -- https://master.php.net/note/delete/97681/spam Del: non-english -- https://master.php.net/note/delete/97681/non-english Del: in docs -- https://master.php.net/note/delete/97681/in+docs Del: other reasons-- https://master.php.net/note/delete/97681 Reject -- https://master.php.net/note/reject/97681 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168865) next »