PHP preg_match_all Function

What is preg_match_all function?

If you want to search and find all the matches of a regular expression pattern from a string, then use preg_match_all() function.

Syntax:

preg_match_all(pattern, subject, matches flags, offset)

Parameters:

pattern (Mandatory): This parameter is a regular expression that you’ll use to find matches in the “subject” parameter (Check Example 1).

subject (Mandatory): This is the main string where you’ll find match (Check Example 1).

matches (Mandatory): If any match found, this parameter is populated with the results (Check Example 1) according to the “flag” parameter which determine population order. The default flag is PREG_PATTERN_ORDER, so if you don’t mention any flag, the function assumes this flag (Check Example 2). This variable becomes a multi-dimensional array.

flags (Optional): You can use one or more flags from the following list to order array elements –

  • PREG_PATTERN_ORDER: If you use this flag, matches[0] becomes an array that contains all the matches found in the “subject” parameter according to the full pattern. matches[1] becomes an array that contains the first captured group’s (or first captured parenthesized pattern’s) result. matches[2] becomes an array that contains the second captured group’s (or second captured parenthesized pattern’s) result and so on (Check Example 3).
  • PREG_SET_ORDER: If you want to group parenthesized pattern with its result, then use this flag. So, matches[0] will contain first group, matches[1] will contain second group, and so on (Check Example 4).
  • PREG_OFFSET_CAPTURE: If you use this flag, the function returns each match as an array whether the “pattern” parameter has groups (Check example 5) or not (Check example 5). Each array has two elements – first element is the matching string found in the “subject” parameter and the second element is the matching position in the “subject” parameter.
  • PREG_UNMATCHED_AS_NULL: If you use this flag, the function returns NULL for each unmatched group used in the “pattern” parameter (Check example 6).

offset (Optional): Instead of starting search from the beginning of the “subject” parameter, if you want to start it from a specific position, mention by this “offset” parameter. (Check example 7).

Return Values:

The function returns-

  • number of full pattern matches. (Check example 3).
  • FALSE if an error occurs.

Examples:

Example 1:

<pre>
<?php
$pattern = '/PHP/';
$subject = "Learn PHP built-in functions.";
if (preg_match_all($pattern, $subject, $matches)) {
    print_r($matches);
}
?>
</pre>

Output:

Array
(
    [0] => Array
        (
            [0] => PHP
        )
)

Example 2:

<pre>
<?php
$pattern = '/PHP/';
$subject = "Learn PHP built-in functions.";
if (preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER)) {
    print_r($matches);
}
?>
</pre>

Output:

Array(
    [0] => Array
        (
            [0] => PHP
        )
 )

Example 3:

<pre>
<?php
$pattern = '/(built-in) (functions)/i';
$subject = "Learn PHP built-in functions. Built-in functions make life easier.";
if ($return_val = preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER)) {
    print_r($matches);
    echo $return_val;
}
?>
</pre>

Output:

Array(
    [0] => Array
        (
            [0] => built-in functions
            [1] => Built-in functions
        )     [1] => Array
        (
            [0] => built-in
            [1] => Built-in
        )
     [2] => Array
        (
            [0] => functions
            [1] => functions
        )
 )
2

Example 4:

<pre>
<?php
$pattern = '/(PHP)/i';
$subject = "Learn PHP built-in functions php.";
if (preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER)) {
    print_r($matches);
}
?>
</pre>

Output:

Array(
    [0] => Array
        (
            [0] => PHP
            [1] => PHP
        )
     [1] => Array
        (
            [0] => php
            [1] => php
        ) 
)

Example 5:

<pre>
<?php
$pattern = '/(PHP) (built-in)/';
$str = "Learn PHP built-in functions.";
if (preg_match_all($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {
    print_r($matches);
}
$pattern = '/PHP/';
$str = "Let's Learn PHP";
if (preg_match_all($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {
    print_r($matches);
}
?>
</pre>

Output:

Array(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => PHP built-in
                    [1] => 6
                )
         )
     [1] => Array
        (
            [0] => Array
                (
                    [0] => PHP
                    [1] => 6
                )
         )
     [2] => Array
        (
            [0] => Array
                (
                    [0] => built-in
                    [1] => 10
                )
         )
 )
Array(    [0] => Array
        (
            [0] => Array
                (
                    [0] => PHP
                    [1] => 12
                )
         )
 )

Example 6:

<pre>
<?php
preg_match_all('/(built)*(function)/', 'PHP built-in function', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);
?>
</pre>

Output:

array(3)
{
  [0]=>
  array(1)
{
    [0]=>    string(8) “function”
  }
  [1]=>
  array(1) {
    [0]=>
    NULL
  }
  [2]=>
  array(1) {
    [0]=>
    string(8) “function”
  }
}

Example 7:

<pre>
<?php
$pattern = '/learn/';
$str = "Do learn PHP built-in functions.";
if (preg_match_all($pattern, $str, $matches, PREG_OFFSET_CAPTURE, 4)) {
    print_r($matches);
}else{
    echo "Match not found";
}
?>
</pre>

Output:

Match not found

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP preg_match_all() Function

preg_match_all() function searches and finds all the matches of a regular expression pattern from a string. It is one of the built-in pcre functions in PHP.

Reference:

https://www.php.net/manual/en/function.preg-match-all.php