2013-10-28 60 views
1

我正在做一個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>'; 

Demo available here

我只想要在我的$matches結果中擁有指定的組。我怎樣才能避免它作爲索引組保存比賽?

回答

1

這是不可能的本地preg_match() - 因爲它實現PCRE和PCRE不能完成。

最簡單的方法是處理輸出數組:

foreach($matches as $key=>$match) 
{ 
    if(is_int($key)) 
    { 
     unset($matches[$key]); 
    } 
}