2013-12-23 28 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