-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcpage.php
50 lines (44 loc) · 1.56 KB
/
calcpage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// requires from https://github.com/andig/php-shunting-yard
require_once "RR/Shunt/Context.php";
require_once "RR/Shunt/Parser.php";
require_once "RR/Shunt/Scanner.php";
require_once "RR/Shunt/Token.php";
require_once "RR/Shunt/Exception/ParseError.php";
require_once "RR/Shunt/Exception/RuntimeError.php";
require_once "RR/Shunt/Exception/SyntaxError.php";
use RR\Shunt\Parser;
header("Content-Type: text/plain");
// allows for + in the url, rather than %2B
if(strpos($_SERVER["QUERY_STRING"], "+") !== false){
$_SERVER["QUERY_STRING"] = str_replace("+", "%2B", $_SERVER["QUERY_STRING"]);
parse_str($_SERVER["QUERY_STRING"], $_GET);
}
$exp_raw = trim($_GET['exp']);
if(empty($exp_raw)){
echo 'empty input';
exit;
} else {
$check = preg_match('#[^0-9()\+-.\/\*\^xeE\ ]#',$exp_raw);
if($check) {
$fail_char = preg_replace('#[0-9()\+-.\/\*\^xeE\ ]#','',$exp_raw);
echo "error, ";
echo implode(array_unique(str_split($fail_char)));
echo " is not valid input.";
exit;
} else {
// sanitize
$exp_san = preg_replace('#[^0-9()\+-.\/\*\^xeE\ ]#','',$exp_raw);
// catch a couple off-cases
$exp_par = str_replace(')(',')*(',$exp_san);
$exp_pow = str_replace('**','^',$exp_par);
$exp_tim = str_replace('x','*',$exp_pow);
$exp_sci = preg_replace('#(\d+\.\d+|\.?\d+)[e|E](-?\+?\d+\.\d+|-?\+?\.?\d+)#','($1*10^($2))',$exp_tim);
$exp_fix = $exp_sci;
// following two lines for debug
//echo $exp_fix;
//echo "\n";
$result = Parser::parse($exp_fix);
echo $result;
}
}