class CondParse
{
protected $syntax;
public $str;
private $strlen;
private $strindex = 0;
private $index = 0;
private $len;
private $tmpCon = 0;
private $or = false;
private $f = false;
private $add = false;
private $lesse = false;
public function __construct($str)
{
$this->str = trim($str);
$this->strlen = strlen($this->str);
$this->syntax = $this->str;
$this->len = $this->strlen;
}
protected function newInstance($str)
{
$p = new self($str);
return $p;
}
public function parse($rev = null)
{
try {
$this->syntax = '';
$this->len = 0;
for (; ;) {
if ($this->strindex >= $this->strlen) {
break;
}
switch ($this->str[$this->strindex]) {
case '(':
$str = false;
$this->strindex += 1;
$count = 0;
while ($this->str[$this->strindex] != ')'
|| $count > 0) {
if ($this->str[$this->strindex] == '(') {
$count++;
} elseif ($this->str[$this->strindex] == ')') {
$count--;
}
$str .= $this->str[$this->strindex++];
}
$this->strindex++;
$p = $this->newInstance($str);
$res = $p->parse();
if ( ! empty(trim($this->syntax))) {
$s = $this->syntax.' '.$res;
// $p = new self($s);
// $res = $p->parse();
} else{
$s = $res;
}
$this->f = true;
$res = $this->parse($s);
return $res;
case '&':
if ($this->str[$this->strindex + 1] != '&') {
throw new \Exception('&& 语法错误!');
}
$this->strindex += 2;
$p = $this->newInstance(strval($rev).' '.$this->syntax);
$left = intval($p->parse());
$opt = '&&';
$right = $this->parserRight();
if($left == false){
return $this->parse($left);
}
$p = $this->newInstance($right);
$right = $p->parse();
$res = $this->exec($left, $right, $opt);
return $this->parse($res);
case '|':
if ($this->str[$this->strindex + 1] != '|') {
throw new \Exception('|| 语法错误!');
}
$this->strindex += 2;
php实现简单条件解析
最新推荐文章于 2022-05-25 18:45:38 发布
本文介绍了如何使用PHP来实现简单的条件解析,支持基础的运算操作。
摘要由CSDN通过智能技术生成