note 114678 added to function.eval
| From: | DanieleMams | Date: | Fri, 21 Mar 2014 09:04:48 +0000 |
| Subject: | note 114678 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198998@lists.php.net to get a copy of this message | ||
Modified version of sooncj answer: your regex not allow something like this: 1-(1). So I modified
your code: I change the 'Allowed PHP functions':
<?php
function calc($equation)
{
// Remove whitespaces
$equation = preg_replace('/\s+/', '', $equation);
echo "$equation\n";
$number = '(?:\d+(?:[,.]\d+)?|pi|Ï)'; // What is a number
$functions =
'(?:abs|atan|ceil|cos|exp|gmp_fact|intval|log(10)?|rand|round|sin|sqrt|tan)'; // Allowed
PHP functions
$operators = '[\/*\^\+-]'; // Allowed math operators
$regexp =
'/^([+-]?('.$number.'|'.$functions.'\s*\((?1)+\)|\((?1)+\))(?:'.$operators.'(?1))?)+$/';
// Final regexp, heavily using recursive patterns
if (preg_match($regexp, $equation))
{
$equation = preg_replace('!pi|Ï!', 'pi()', $equation); // Replace pi
with pi function
echo "$equation\n";
eval('$result = '.$equation.';');
}
else
{
$result = false;
}
return $result;
}
?>
This function allow also this 'strange' math function:
1) a-+b
2) a+-b
3) a++b << PHP not allow this
4) a--b. << PHP not allow this
But not allow with 3 '+' or '-':
1) a---b, a+++b, a-++b, a+--b, a+-+b, a-+-b.
Allow other Math functions.
For a++b or a--b i write another function that block that!
----
Server IP: 72.52.91.14
Probable Submitter: 83.211.186.110
----
Manual Page -- http://php.net/manual/en/function.eval.php
Edit -- https://master.php.net/note/edit/114678
Del: integrated -- https://master.php.net/note/delete/114678/integrated
Del: useless -- https://master.php.net/note/delete/114678/useless
Del: bad code -- https://master.php.net/note/delete/114678/bad+code
Del: spam -- https://master.php.net/note/delete/114678/spam
Del: non-english -- https://master.php.net/note/delete/114678/non-english
Del: in docs -- https://master.php.net/note/delete/114678/in+docs
Del: other reasons-- https://master.php.net/note/delete/114678
Reject -- https://master.php.net/note/reject/114678
Search -- https://master.php.net/manage/user-notes.php