Tyrick
Tyrick

Reputation: 2966

How to grab all regex matches in php?

$pattern = '/b+[^\s-]*/';
$subject = 'brew and this is bunny';
preg_match($pattern, $subject, $matches);

Clearly, brew and bunny should match, but $matches only contains brew.

What should I do to have $matches contain all regex matches

Upvotes: 0

Views: 59

Answers (1)

hwnd
hwnd

Reputation: 70732

You don't need the + after b in your regex.

$str = 'brew and this is bunny';
preg_match_all('/b[^\s]+/', $str, $matches);
print_r($matches);

Outputs

Array
(
    [0] => Array
        (
            [0] => brew
            [1] => bunny
        )
)

Upvotes: 1

Related Questions