PHP preg_match_all() Function
PHP Regular Expressions (PCRE)
The preg_match_all function performs a global regular expression match.
Syntax
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
Searches subject for all matches to the regular expression given by pattern and outputs them to matches in the order specified by flag.
After the first match is found, the subsequence continues searching from the position of the last match.
Parameter Description:
-
$pattern: the pattern to search for, as a string.
-
$subject: the input string.
-
$matches: a multi-dimensional array, used as an output parameter to output all match results; the array ordering is specified by flags.
-
$flags: can be combined with the following flags (note that PREG_PATTERN_ORDER and PREG_SET_ORDER cannot be used at the same time):
PREG_PATTERN_ORDER: The results are ordered so that $matches[0] holds all matches of the complete pattern, $matches[1] holds all matches of the first subgroup, and so on.
PREG_SET_ORDER: The results are ordered so that $matches[0] contains all matches (including subgroups) from the first match, $matches[1] is an array containing all matches (including subgroups) from the second match, and so on.
PREG_OFFSET_CAPTURE: If this flag is passed, every match found will be returned with its offset relative to the target string.
PREG_PATTERN_ORDER: The results are ordered so that $matches[0] holds all matches of the complete pattern, $matches[1] holds all matches of the first subgroup, and so on.
2.PREG_SET_ORDER: The results are ordered so that $matches[0] contains all matches (including subgroups) from the first match, $matches[1] is an array containing all matches (including subgroups) from the second match, and so on.
3.PREG_OFFSET_CAPTURE: If this flag is passed, every match found will be returned with its offset relative to the target string.
-
offset: Normally, the search starts from the beginning of the target string. The optional offset parameter is used to start searching from a specified position in the target string (in bytes).
Return Value
Returns the number of complete matches (which may be 0), or FALSE if an error occurs.
Example
Find matches The difference between the content of the tags:
PHPTitle: Programming Language " ; preg_match_all ( " /(.*)<\/b>/U " , $userinfo , $pat_array ) ; print_r ( $pat_array [ 0 ] ) ; ?>
The execution result is as follows:
Array
(
[0] => PHP
[1] => Programming Language
)
Find matching HTML tags (greedy):
bold textclick me " ; preg_match_all ( " /(<([\w]+)[^>]*>)(.*?)(<\/ \\ 2>)/ " , $html , $matches , PREG_SET_ORDER ) ; foreach ( $matches as $val ) { echo " matched: " . $val [ 0 ] . " \n " ; echo " part 1: " . $val [ 1 ] . " \n " ; echo " part 2: " . $val [ 2 ] . " \n " ; echo " part 3: " . $val [ 3 ] . " \n " ; echo " part 4: " . $val [ 4 ] . " \n \n " ; } ?>The execution result is as follows:
matched: bold text
part 1:
part 2: b
part 3: bold text
part 4:
matched: click me
part 1:
part 2: a
part 3: click me
part 4: