PHP中的动态逻辑表达式解析/评估?

编程入门 行业动态 更新时间:2024-10-13 14:23:55
本文介绍了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中的动态逻辑表达式解析/评估?

本文发布于:2023-11-12 10:29:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   逻辑   动态   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!