PHP中的动态逻辑表达式解析/评估? [英] Dynamic logical expression parsing/evaluation in PHP?

查看:196
本文介绍了PHP中的动态逻辑表达式解析/评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要评估一些PHP页面上任意复杂度的用户定义逻辑表达式.假设表单字段是主要变量,则需要:

I have a need to evaluate user-defined logical expressions of arbitrary complexity on some PHP pages. Assuming that form fields are the primary variables, it would need to:

  • 用变量"代替表格 字段值;
  • 处理比较运算符, 至少==,<,< =,> =和>由 符号,名称(例如eq,lt,le,ge,gt 分别);
  • 不处理布尔运算符,和,或或 可能是按名称,符号(例如,!, && ;, ||和^^);
  • 处理字符串的文字值 和数字;
  • 为纯文本而非XML(例如名字" ==''或姓氏=='');和
  • 表现合理.
  • substitute"varibles" for form fields values;
  • handle comparison operators, minimally ==, <, <=, >= and > by symbol, name (eg eq, lt, le, ge, gt respectively);
  • handle boolean operators not, and, or and possibly xor by name, symbol (eg !, &&, || and ^^ respectively);
  • handle literal values for strings and numbers;
  • be plaintext not XML (eg "firstname == '' or lastname == ''); and
  • be reasonably performant.

现在,在过去的几年中,我已经编写了递归下降解析器,可以构建表达式树并执行此类操作,但这并不是我要用PHP来完成的任务,因此我希望那里有能够解决的问题至少可以带我去那里.

Now in years gone by I've written recursive descent parsers that could build an expression tree and do this kind of thing but thats not a task I'm relishing in PHP so I'm hoping there are things out there that will at least get me some of the way there.

建议?

推荐答案

检查 create_function ,它会创建来自传递的字符串参数的匿名函数,我不确定它的性能,但是它非常灵活...

Check create_function, it creates an anonymous function from the string parameters passed, I'm not sure about its performance, but it's very flexible...

这篇关于PHP中的动态逻辑表达式解析/评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆