exexe
exexe

Reputation: 196

Regex is returning only the first word from text

I have problem with my regex expression.
My regex return only first word from text. I want to return all word from this string.

Regex:

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

Returned code from var_dump($reason):

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

I want:

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

Upvotes: 0

Views: 52

Answers (1)

Robin
Robin

Reputation: 9644

\w doesn't match whitespaces, only alphanumerical characters. That's why it stops when encountering the first .

If everything is text after the :, you may want to use

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

Upvotes: 1

Related Questions