如果我沒有理解好你的問題,你想知道如何查找存儲在preg_match_all
第三個參數的結果。
preg_match_all
將結果作爲二維數組存儲在第三個參數中。
兩個結構是可能的,並且可以在第四個參數中用兩個常量顯式設置。
比方說,你有兩個拍攝組的模式和你的主題字符串包含3次出現的模式:
1)PREG_PATTERN_ORDER
是返回類似的東西默認設置:
[
0 => [ 0 => 'whole match 1',
1 => 'whole match 2',
2 => 'whole match 3' ],
1 => [ 0 => 'capture group 1 from match 1',
1 => 'capture group 1 from match 2',
2 => 'capture group 1 from match 3' ],
2 => [ 0 => 'capture group 2 from match 1',
1 => 'capture group 2 from match 2',
2 => 'capture group 2 from match 3' ]
]
2 )PREG_SET_ORDER
返回類似的東西:
[
0 => [ 0 => 'whole match 1',
1 => 'capture group 1 from match 1',
2 => 'capture group 2 from match 1' ],
1 => [ 0 => 'whole match 2',
1 => 'capture group 1 from match 2',
2 => 'capture group 2 from match 2' ],
2 => [ 0 => 'whole match 3',
1 => 'capture group 1 from match 3',
2 => 'capture group 2 from match 3' ]
]
具體的例子可以在找到。你只需要選擇哪一個更適合你需要做的選擇。因此,要應用您的功能,您只需執行foreach
循環或使用array_map
(這也是一個隱藏循環,但更短更慢)。因此,要應用您的功能,您只需執行foreach
循環或使用array_map
即可。
一般來說,如果您不確定變量的結構是什麼,您可以使用print_r
或var_dump
來了解它的外觀。
我想拿起,除塵,並回答這個問題。你能否提供(作爲你的問題的一個編輯)一個最小的,相關的'$ contents'可以包含的樣本?你能解釋一下你想要捕捉哪些部分嗎?有了這些新信息,我可以嘗試爲您提供解決方案。更新問題後請ping我。 – mickmackusa