php实现简单条件解析

本文介绍了如何使用PHP来实现简单的条件解析,支持基础的运算操作。
摘要由CSDN通过智能技术生成
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;
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值