2015-08-22 24 views
1

今天,我已經閱讀堆棧溢出的問題,在這裏:https://stackoverflow.com/questions/32151810/how-to-parse-this-text-block-into-variables-by-php我自己的PHP正則表達式代碼有什麼錯誤?

我曾嘗試使用此代碼做到這一點:

preg_match("/([a-zA-Z_]+)/", "[first_text] = [second_text, third_text] : [forth_text, fifth_text]", $matches); 

當我測試了它,它並沒有正常工作:

echo $matches[0]; 
echo $matches[1]; 
echo $matches[2]; 
echo $matches[3]; 
echo $matches[4]; 

會打印:

first_textfirst_text 

我自己的PHP正則表達式代碼中的錯誤是什麼?

回答

3

您需要使用:

preg_match_all('/([a-zA-Z_]+)/', "[first_text] = [second_text, third_text] : [forth_text, fifth_text]", $matches); 

preg_match_all會使用您正則表達式返回所有的比賽,而preg_match只是給你的第一場比賽。