English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP preg_match_all() Function Usage and Example

PHP Regular Expression (PCRE)

The preg_match_all function is used to perform 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 for all matches of the pattern given by the regular expression in subject and outputs them to matches in the order specified by flag.

After the first match is found, the subsequent sequence continues to search from the position of the last match.

Parameter Description:

  • $pattern: The pattern to be searched, in string form.

  • $subject: The input string.

  • $matches: A multidimensional array, which outputs all matching results as an output parameter, and the array sorting 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 sorted so that $matches[0] saves all matches of the complete pattern, $matches[1saves all matches of the first subgroup, and so on.

  • PREG_SET_ORDER: The results are sorted so that $matches[0] contains all matches (including subgroups) obtained from the first match, $matches[1is an array containing all matches (including subgroups) found for the second time, and so on.

  • PREG_OFFSET_CAPTURE: If this flag is passed, the offset relative to the target string is added to each match returned.

  • offset: Generally, the search starts from the beginning of the target string. The optional parameter offset is used to start searching from a specified position in the target string (in bytes).

Return value

Returns the total number of complete matches (could be 0), or FALSE if an error occurs.

Online Examples

<?php
$userinfo = "Name: <b>PHP</b>/b> <br> Title: <b>Programming Language</b>/b>"
preg_match_all ("/<b>(.*)<//b>/U", $userinfo, $pat_array);
print_r($pat_array[0]);
?>

The execution result is as follows:

Array
(
    [0] => <b>PHP</b>/b>
    [1] => <b>Programming Language</b>/b>
)
<?php
//\\2is an example of a backward reference. This tells pcre that it must match the second parenthesis in the regular expression (here ([\w+)
//Matching results. The use of two backslashes here is because double quotes are used here.
$html = "<b>bold text</b>/b><a href=howdy.html>click me</a>/a>"
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: <b>bold text</b>/b>
part 1: <b>
part 2: b
part 3: bold text
part 4: </b>
matched: <a href=howdy.html>click me</a>/a>
part 1: <a href=howdy.html>
part 2: a
part 3: click me
part 4: </a>

PHP Regular Expression (PCRE)