2013-07-26 160 views
0

我有這樣的:我怎樣才能讓preg_match_all陣列

$text = $_POST['text']; 

當我echo $text我得到這個:

ggg #hhh #ddd ggg hhhrr ggg #ttt 

當我這樣做:

$val = preg_match_all("/#\w+/", $text, $matches); 
print_r($matches); 

我得到

Array ([0] => Array ([0] => #hhh [1] => #ddd [2] => #ttt)) 

但我想這樣的輸出:

print_r($matches); 

要這樣:

Array ([0] => #hhh [1] => #ddd [2] => #ttt) 

感謝

回答

0

改變這種以從數組的第一個(零)項目

print_r($matches[0]); 
1

另一種方法是使用命名組。

$val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches); 
print_r($matches['myLabel']);