如果我没有理解好你的问题,你想知道如何查找存储在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