Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式只返回文本中的第一个单词_Php_Regex - Fatal编程技术网

Php 正则表达式只返回文本中的第一个单词

Php 正则表达式只返回文本中的第一个单词,php,regex,Php,Regex,我的正则表达式有问题。 我的正则表达式只返回文本中的第一个单词。我想返回这个字符串中的所有单词 正则表达式: $test = "Reason: test test test"; $regex = "/Reason: (\w+)+/"; preg_match_all($regex, $test, $reason); 从变量转储($reason)返回的代码 array(2) { [0]=> array(1) { [0]=> str

我的正则表达式有问题。
我的正则表达式只返回文本中的第一个单词。我想返回这个字符串中的所有单词

正则表达式:

$test = "Reason: test test test";
$regex = "/Reason: (\w+)+/";
preg_match_all($regex, $test, $reason);
从变量转储($reason)返回的代码

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"
    }
}

\w
与空格不匹配,仅与字母数字字符匹配。这就是它遇到第一个
时停止的原因

如果
后面的所有内容都是文本,则您可能需要使用

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

*$
。。。。。。。。。。。。。。。。。。谢谢你的解决方案@zerkms,这就是我需要的。