我需要评估一些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中的动态逻辑表达式解析/评估?
发布评论