2013-12-19 146 views
1

我需要使用php解析單引號文本(注意多個嵌套=單個),類似於論壇引用標記。例如:正則表達式引用標籤php

some nonquoted text1 
[quote="person1"]some quoted text11[/quote] 
some nonquoted text2 
[quote="person2"]some quoted text22[/quote] 
etc... with no newlines necessarily... 

結果應該是像數組

Array 
     (
      ['nonquoted'] => Array 
       (
        [0] => some unquoted text1 
        [1] => some unquoted text2 
       ) 
      ['quoted'] => Array 
       { 
        [0] => Array 
         (
          [0] => person1 
          [1] => some quoted text11 
         ) 

        [1] => Array 
         (
          [0] => person2 
          [1] => some quoted text22 
         ) 
       } 
     } 
+0

...和你嘗試過什麼? –

+0

下面是一個讓你開始的例子,它只會匹配引用的內容,但它肯定不會創建你以後的漂亮結構:http://regex101.com/r/mN0yL1 –

+0

索引數組應該是用於統一數據,異構數據應該是關聯數組。因此'$ array ['quoted']'中的內部數組應該像'Array('name'=>'person1','text'=>'some quoted text11')'。 – Barmar

回答

0
$input= <<<EOL 
some nonquoted text1 
[quote="person1"]some quoted text11[/quote] 
some nonquoted text2 
[quote="person2"]some quoted text22[/quote] 
EOL; 

$result = Array('unquoted'=>Array(), 'quoted'=>Array()); 

//find [quote] blocks, replace them with nothing, and store the text in $result['quoted'] 
$unquoted = preg_replace_callback('@\[quote="([^\"]+)"\](.*)\[/quote\]@',function($m) use(&$result){ 
    $result['quoted'][]=Array($m[1],$m[2]); 
},$input); 

//what's left is only unquoted lines, so split them into an array 
$result['unquoted']=preg_split('@[\r\n][email protected]',$unquoted); 

//your result 
print_r($result); 
+0

就是這樣。謝謝肖恩。 –