2013-12-23 27 views
0

我问的问题关于正则表达式替换引用,不带引号,引用,不带引号的......在这个线程情况:Regex for quote tags php报价标签的正则表达式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 
       ) 

       ... 
     } 
+3

嘿,你告诉我们你试过什么? – HamZa

回答

0

正则表达式是解决这个问题的一个不错的选择,因为他们不请保持状态的能力。您提到嵌套时所指的那种状态。即使对于不允许嵌套标签的简单方法,解决方案仍然依赖于了解表达式的状态,因为您希望根元素被标识为(,对于您期望的数组中的第一个元素)。

相反,更好的解决方案是使用已经证明的快速单通解析器,如BBcode,它可以做更有效的工作并提供更多可维护的代码。

+0

PCRE比你想象的要强大得多,请看[正则表达式的强大](http://nikic.github.io/2012/06/15/The-true-power-of-regular-expressions.html)。我已经得到了这个问题的正则表达式解决方案,但我们不应该提供[helpvampire](http://slash7.com/2006/12/22/vampires/)。 – HamZa