2016-11-29 82 views
1

我使用PHP preg_match_all提取這樣的消息的某些部分:PHP preg_match_all沒有得到所有的結果

$customerMessage = '"message":"success:2,2;3,3;"' ; 
preg_match_all('/("message":")([a-z0-9A-Z]+):([0-9]+,[0-9]+;)+/', $customerMessage, $matches); 
var_dump($matches); 
die; 

這段代碼的輸出是:

array(4) { 
    [0]=> 
    array(1) { 
    [0]=> 
    string(27) ""message":"success:2,2;3,3;" 
    } 
    [1]=> 
    array(1) { 
    [0]=> 
    string(11) ""message":"" 
    } 
    [2]=> 
    array(1) { 
    [0]=> 
    string(7) "success" 
    } 
    [3]=> 
    array(1) { 
    [0]=> 
    string(4) "3,3;" 
    } 
} 

爲什麼不能我得到部分2,2;? 在此先感謝!

回答

3

你只能得到一組的最後一場比賽。二送樣x,x;你可以使用當前的正則表達式所有值,改了一下:

preg_match_all('/("message":")([a-z0-9A-Z]+):(.*)"/', $customerMessage, $matches); 
/* $matches[3] --> 2,2;3,3; 

現在你可以組3 $matches[3]和匹配所有x,x;[0-9]+,[0-9]+;

preg_match_all('/[0-9]+,[0-9]+/', $matches[3], $matches2); 
/* $matches[0] --> 2,2; 
/* $matches[1] --> 3,3; 
+0

你只能得到最後的一個組的匹配!謝謝。你是對的! – Abadis