2017-04-11 142 views
1

如果我提出的正則表達式,並通過陣列$content它:Preg_match_all也返回初始值

preg_match_all(/([0-9]+)\/([0-9]+)/, $content, $matches); 

哪裏$content是數組:

Name Address 77/88 Country 
Name Address 71/90 Country 
Name Address 72/43 Country 
Name Address 76/55 Country 
Name Country 
Name Address 

它將返回$matches

array(4) { 
    [0]=> 
    string(5) "77/88" 
    [1]=> 
    string(5) "71/90" 
    [2]=> 
    string(5) "72/43" 
    [3]=> 
    string(5) "76/55" 
    } 

但我能不知何故最初的數組$content瓦爾你也有匹配的值嗎?

+0

預期結果是什麼? – Toto

回答

0

$content將需要爲preg_match_all()的字符串:

preg_match_all('/^.*?([0-9]+)\/([0-9]+).*$/m', $content, $matches); 

產量:

Array 
(
    [0] => Array 
     (
      [0] => Name Address 77/88 Country 
      [1] => Name Address 71/90 Country 
      [2] => Name Address 72/43 Country 
      [3] => Name Address 76/55 Country 
     ) 

    [1] => Array 
     (
      [0] => 77 
      [1] => 71 
      [2] => 72 
      [3] => 76 
     ) 

    [2] => Array 
     (
      [0] => 88 
      [1] => 90 
      [2] => 43 
      [3] => 55 
     ) 
) 

如果$content真的是一個數組:

$matches = preg_grep('/([0-9]+)\/([0-9]+)/', $content); 

產量:

Array 
(
    [0] => Name Address 77/88 Country 
    [1] => Name Address 71/90 Country 
    [2] => Name Address 72/43 Country 
    [3] => Name Address 76/55 Country 
)