PREG_MATCH_ALL
Perform a global regular expression match
SYNTAX
preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] ) : int
PARAMETERS
pattern
The pattern to search for, as a string.
subject
The input string.
matches
Array of all matches in multi-dimensional array ordered according to flags.
flags
Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):
PREG_PATTERN_ORDER | Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. If the pattern contains named subpatterns, $matches additionally contains entries for keys with the subpattern name. If the pattern contains duplicate named subpatterns, only the rightmost subpattern is stored in $matches[NAME]. |
PREG_SET_ORDER | Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. |
PREG_OFFSET_CAPTURE | If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array of arrays where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1. |
PREG_UNMATCHED_AS_NULL | If this flag is passed, unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string. |
offset
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
Note: Using offset is not equivalent to passing substr($subject, $offset) to preg_match_all() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). See preg_match() for examples.
RETURN
Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
EXAMPLES
PATTERN SUBJECT | CASE-SENSITIVE
0
PATTERN SUBJECT | CASE-INSENSITIVE
2
MATCHES
Array
(
[0] => Array
(
[0] => PHP
[1] => PHP
)
)
FLAGS | PREG_PATTERN_ORDER
Array
(
[0] => Array
(
[0] => <p>paragraph</p>
[1] => <span>span</span>
)
[1] => Array
(
[0] => paragraph
[1] => span
)
)
FLAGS | PREG_PATTERN_ORDER | NAMED SUBPATTERN
Array
(
[0] => Array
(
[0] => named
[1] => subpattern
)
[match] => Array
(
[0] =>
[1] => subpattern
)
[1] => Array
(
[0] => named
[1] =>
)
[2] => Array
(
[0] =>
[1] => subpattern
)
)
FLAGS | PREG_SET_ORDER
Array
(
[0] => Array
(
[0] => <p>paragraph</p>
[1] => paragraph
)
[1] => Array
(
[0] => <span>span</span>
[1] => span
)
)
FLAGS | PREG_OFFSET_CAPTURE
Array
(
[0] => Array
(
[0] => Array
(
[0] => pregoffsetcapture
[1] => 0
)
)
[1] => Array
(
[0] => Array
(
[0] => preg
[1] => 0
)
)
[2] => Array
(
[0] => Array
(
[0] => offset
[1] => 4
)
)
[3] => Array
(
[0] => Array
(
[0] => capture
[1] => 10
)
)
)
FLAGS | PREG_UNMATCHED_AS_NULL
array (
0 =>
array (
0 => 'pregas',
),
1 =>
array (
0 => 'preg',
),
2 =>
array (
0 => '',
),
3 =>
array (
0 => 'as',
),
)
array (
0 =>
array (
0 => 'pregas',
),
1 =>
array (
0 => 'preg',
),
2 =>
array (
0 => NULL,
),
3 =>
array (
0 => 'as',
),
)
OFFSET
Array
(
[0] => Array
(
[0] => Array
(
[0] => offset
[1] => 7
)
[1] => Array
(
[0] => offset
[1] => 14
)
)
)
PHONE
Array
(
[0] => Array
(
[0] => 555-1212
[1] => 800-555-1212
)
[1] => Array
(
[0] =>
[1] => 800
)
)
NAMED SUBPATTERN
Array
(
[0] => Array
(
[0] => a: 1
[1] => b: 2
[2] => c: 3
)
[name] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[digit] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
HTML
Array
(
[0] => Array
(
[0] => <b>bold</b>
[1] => <b>
[2] => b
[3] => bold
[4] => </b>
)
[1] => Array
(
[0] => <a href="#">anchor</a>
[1] => <a href="#">
[2] => a
[3] => anchor
[4] => </a>
)
)