我正在preg_match
使用命名捕获组。当我打印出$matches
它时,它会显示命名组,以及默认的索引组,如下所示:
Array
(
[0] => placeholder/placeholder2
[p1] => placeholder <-- Named, good
[1] => placeholder <-- Indexed, don't want this
[p2] => placeholder2 <-- Named, good
[2] => placeholder2 <-- Indexed, don't want this
)
使用这段代码:
$str = 'placeholder/placeholder2';
preg_match('#(?P<p1>[[:alnum:]]+)/(?P<p2>[[:alnum:]]+)#', $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
我只想在我的$matches
结果中包含命名组。如何避免将匹配项也保存为索引组?