Php 不要保存索引捕获的组,只保存命名的组
我正在与命名的捕获组进行Php 不要保存索引捕获的组,只保存命名的组,php,regex,preg-match,Php,Regex,Preg Match,我正在与命名的捕获组进行预匹配。当我打印出$matches时,它会显示命名组,但也会显示默认索引组,如下所示: Array ( [0] => placeholder/placeholder2 [p1] => placeholder <-- Named, good [1] => placeholder <-- Indexed, don't want this [p2] => placeholder2 <-- Named
预匹配。当我打印出$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
)
数组
(
[0]=>占位符/占位符2
[p1]=>占位符占位符这不可能在本机使用-因为它实现了PCRE,而PCRE无法实现这一点
最简单的方法就是处理输出数组:
这是不可能在本地实现的,因为它实现了PCRE,而PCRE无法实现这一点
最简单的方法就是处理输出数组:
$str = 'placeholder/placeholder2';
preg_match('#(?P<p1>[[:alnum:]]+)/(?P<p2>[[:alnum:]]+)#', $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
foreach($matches as $key=>$match)
{
if(is_int($key))
{
unset($matches[$key]);
}
}