我已经在这个主题中询问有关正则引用的替代引用,引用,引用,不引用...的案例的问题:Regex for quote tags php
我意识到我需要更多关于保留物品订单的一般情况。像引用,引用,引用,引用,引用...等...还有不嵌套引号。我想它应该迭代或递归...我会在例子上解释。
some unquoted text11
[quote="person1"]some quoted text11[/quote]
[quote="person2"]some quoted text22[/quote]
[quote="person3"]some quoted text33[/quote]
some unquoted text22
...
[quote="person4"]some quoted text44[/quote]
...
结果数组应为:
Array //PRESERVED ORDER
(
[0] => Array
(
['type'] => unquoted
['name'] => ''
['text'] => some unquoted text11
)
[1] => Array
(
['type'] => quoted
['name'] => person1
['text'] => some quoted text11
)
[2] => Array
(
['type'] => quoted
['name'] => person2
['text'] => some quoted text22
)
[3] => Array
(
['type'] => quoted
['name'] => person3
['text'] => some quoted text33
)
[4] => Array
(
['type'] => unquoted
['name'] => ''
['text'] => some unquoted text22
)
...
[5] => Array
(
['type'] => quoted
['name'] => person4
['text'] => some quoted text44
)
...
}
答案 0 :(得分:0)
正则表达式是解决此问题的不良选择,因为它们不能为您提供维护状态的能力。当你提到嵌套时,你所指的状态。即使对于不允许嵌套标记的简单方法,解决方案仍然依赖于知道表达式的状态,因为您希望根元素被识别为这样(根据预期数组中的第一个元素)。
相反,更好的解决方案是使用已经过验证且快速的单通道解析器,如BBcode,它可以做更有效的工作并提供更易维护的代码。