2014-01-23 30 views
0

我遇到了我的正則表達式問題。
我的正則表達式只返回來自文本的第一個單詞。我想從這個字符串中返回所有的單詞。正則表達式僅返回文本中的第一個單詞

正則表達式:

$test = "Reason: test test test"; 
$regex = "/Reason: (\w+)+/"; 
preg_match_all($regex, $test, $reason); 
的var_dump($原因)

返回代碼:

array(2) { 
    [0]=> 
    array(1) { 
     [0]=> 
      string(12) "Reason: test" 
    } 
    [1]=> 
    array(1) { 
     [0]=> 
      string(4) "test" 
    } 
} 

我想:

array(2) { 
    [0]=> 
    array(1) { 
     [0]=> 
     string(12) "Reason: test test test" 
    } 
    [1]=> 
    array(1) { 
     [0]=> 
     string(4) "test test test" 
    } 
} 
+2

'* $'? .................. – zerkms

+0

感謝您的解決方案@zerkms,這是我需要的。 – exexe

回答

1

\w不匹配空格,只有字母數字字符。這就是爲什麼當遇到第一個時停止。

如果一切:後的文本,您可能需要使用

$regex = "/Reason: (.+)/" 
+0

工作得很好,感謝解決方案@Robin。 – exexe

相關問題