In PHP, preg_match_all() finds every match of a regular expression in a string and stores them in $matches.
It returns the number of full matches, or false on error. By default (PREG_PATTERN_ORDER) $matches[0] holds all full matches, $matches[1] all of group 1, and so on. With PREG_SET_ORDER, each $matches[n] is one match with its groups.
Syntax
Call preg_match_all() with the values you need. Start with this line.
PHP
$n = preg_match_all('/\w+/', $text, $matches);
Call
What it does
preg_match_all('/\d+/', 'a1 b22')
Returns 2 and can fill $matches with "1" and "22".
preg_match_all('/(\w+)@(\w+)/', $s, $m)
Fills full matches and each capturing group for every hit.
preg_match_all($p, $s, $m, PREG_SET_ORDER)
Groups each match as its own array row.
PREG_PATTERN_ORDER (default) is handy when you want one column per group. PREG_SET_ORDER is handy when you loop match-by-match as a set of fields.
Library declarations
PHP
preg_match_all(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false
Parameters
Parameter
Description
pattern
Required. PCRE pattern with delimiters and optional flags.
subject
Required. The string to search.
matches
Optional. Passed by reference. Receives all matches, shaped by $flags.