如何解析PHP中类似于BBCode的伪代码?
我正在使用包含以下行的模板文件:
[field name="main_div" type='smallblock' required="yes"]
[field type='bigblock' color="red" name="inner_div"]
[field name="btn" type='button' caption='Submit']
与 HTML 行混合。
它是根据属性值生成html代码的伪代码。
我的属性集有限,但不控制它们在字符串中的顺序以及所有属性的存在。例如,有时会设置“必需”属性,有时会丢失。
解析此类字符串的最简单方便的方法是什么,以便我可以将属性用作关联数组?
正则表达式、有限状态机、从[到]获取子串、按空格爆炸、按等号爆炸?
寻找可与提供的示例一起使用的建议或简单的代码。
I am working with template files that contain lines like these:
[field name="main_div" type='smallblock' required="yes"]
[field type='bigblock' color="red" name="inner_div"]
[field name="btn" type='button' caption='Submit']
mixed with HTML lines.
It's pseudocode for html code generation according to attribute values.
I have limited set of attributes, but don't control their order in string and presence of all of them. Sometimes "required" attribute is set, sometimes is missed, for example.
What is the easiest and convenient way to parse such strings, so I can work with attributes as associative array?
Regular expression, finite state machine, get substring from [ to ], explode by space and explode by equal sign?
Looking for advice or simple piece of code that can work with provided example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正则表达式s。虽然您可以为这样的方案编写解析器,但它太过分了,并且无法提供针对乱码标记的弹性。
诀窍是使用两个正则表达式,一个用于查找
[field]
标记,第二个用于拆分属性。Regular expressions. While you could write a parser for schemes like this, it's overkill and provides no resiliency against garbled tokens.
The trick is to use two regular expressions, one for finding the
[field]
tokens and a second to split out the attributes.