2010-12-03 56 views
0

,我渴望尋求的解決方案得到這個文本字符串PHP - > preg_match_all爲以下結構<h6>我的標題</h6>一些文字... <h6>另一個標題</h6>更多的文字

<h6>First pane</h6> 
... pane content ... 
<h6>Second pane</h6> 
Hi, this is a comment. 
To delete a comment, just log in and view the post's comments. 
There you will have the option to edit 
or delete them. 
<h6>Last pane</h6> 
... last pane content ... 

解析到一個PHP數組。

我需要把它單獨給

1. 
1.0=> First pane 
1.1=> ... pane content ... 

2. 
2.0=> Second pane 
2.1=> Hi, this is a comment. 
    To delete a comment, just log in and view the post's comments. 
    There you will have the option to edit 
    or delete them. 

3. 
3.0=> Last pane 
3.1=> ... last pane content ... 
+1

*(相關)* [解析HTML的最佳方法](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon 2010-12-03 16:08:48

回答

1

你的正則表達式應該是這樣的:

/<h6>([^<]+)<\/h6>([^<]+)/im 

如果您運行下面的腳本,你會發現你正在尋找的值在$匹配[1]和$匹配[2]。

$s = "<h6>First pane</h6> 
... pane content ... 
<h6>Second pane</h6> 
Hi, this is a comment. 
To delete a comment, just log in and view the post's comments. 
There you will have the option to edit 
or delete them. 
<h6>Last pane</h6> 
... last pane content .."; 
$r = "/<h6>([^<]+)<\/h6>([^<]+)/im"; 

$matches = array(); 
preg_match_all($r,$s,$matches); 

print_r($matches); 
+0

謝謝。這幾乎奏效。我只是想我的例子中的實際內容,我把它命名爲1.1,2.1和3.1。任何想法我怎麼能得到那個...? – chris 2010-12-03 16:31:05

+0

嗨,我很抱歉你的代碼有效。我抄了你的變量$ S的內容和它的工作.... Unfortunaly我檢查我的$ S的輸入,它看起來像這樣

第一個窗格

…窗格中的內容…

第二個窗格

嗨,這是一條評論。
要刪除評論,請登錄和查看帖子’的評論。
在那裏您可以選擇編輯
或刪除它們。

最後一個窗格

…最後一個窗格內容…
任何想法如何才能得到這個工作? – chris 2010-12-03 16:42:00

0

我相信PREG_SET_ORDER標誌是你在找什麼。

$regex = '~<h6>([^<]+)</h6>\s*([^<]+)~i'; 

preg_match_all($regex, $source, $matches, PREG_SET_ORDER); 

這種方式,在每個$元素相匹配陣列是包含總體匹配,隨後所有的組的陣列捕捉單個匹配嘗試。結果直到第一場比賽看起來是這樣的:

Array 
(
    [0] => Array 
     (
      [0] => First pane 
... pane content ... 

      [1] => First pane 
      [2] => ... pane content ... 

     )

see it in action on ideone

編輯:請注意\s*我加了。沒有這一點,匹配的內容始終沒有行分隔符開始。

相關問題