forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField_calculate.php
87 lines (72 loc) · 2.17 KB
/
Field_calculate.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
/**
* This class describes a field calculate.
* reff : https://stackoverflow.com/a/27077376/10351006
*
*
* how to use class->calculate(input string)
*/
class Field_calculate
{
const PATTERN = '/(?:\-?\d+(?:\.?\d+)?[\+\-\*\/])+\-?\d+(?:\.?\d+)?/';
const PARENTHESIS_DEPTH = 10;
public function __construct()
{
}
public function calculate($input)
{
if (strpos($input, '+') != null || strpos($input, '-') != null || strpos($input, '/') != null || strpos($input, '*') != null)
{
// Remove white spaces and invalid math chars
$input = str_replace(',', '.', $input);
$input = preg_replace('[^0-9\.\+\-\*\/\(\)]', '', $input);
// Calculate each of the parenthesis from the top
$i = 0;
while (strpos($input, '(') || strpos($input, ')'))
{
$input = preg_replace_callback('/\(([^\(\)]+)\)/', 'self::callback', $input);
$i++;
if ($i > self::PARENTHESIS_DEPTH)
{
break;
}
}
// Calculate the result
if (preg_match(self::PATTERN, $input, $match))
{
return $this->compute($match[0]);
}
// To handle the special case of expressions surrounded by global parenthesis like "(1+1)"
if (is_numeric($input))
{
return $input;
}
return 0;
}
return $input;
}
private function compute($input) {
// only for php v5.6
// deprecated on php v7.2+
//
// $compute = create_function('', 'return ' . $input . ';');
// return 0 + $compute;
// support php 5.6+
return 0 + eval('return '.$input.';');
}
private function callback($input)
{
if (is_numeric($input[1]))
{
return $input[1];
}
elseif (preg_match(self::PATTERN, $input[1], $match))
{
return $this->compute($match[0]);
}
return 0;
}
}