PHP preg_match_all() Function: Usage, Parameters, and Examples

This article explains the PHP preg_match_all() function, detailing its signature, purpose, parameter descriptions, return values, and provides three practical code examples that demonstrate pattern matching, result extraction, and named sub‑pattern usage.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP preg_match_all() Function: Usage, Parameters, and Examples

The preg_match_all() function performs a global regular‑expression match on a subject string, returning all matches in an array.

Signature:

int preg_match_all(string $pattern, string $subject, array &$matches, int $flags = 0, int $offset = 0)

Parameters: $pattern – the regex pattern to search for. $subject – the input string. $matches – an output array that receives the matches; its ordering is controlled by $flags. $flags – optional flags such as PREG_PATTERN_ORDER or PREG_SET_ORDER (cannot be combined). $offset – optional byte offset to start the search.

Return value: the number of full pattern matches (or FALSE on error).

Example 1 – Matching phone numbers:

<?php
preg_match_all("/\(?(\d{3})?\)?(?(1)[\-\s])\d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones);
print_r($phones);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => 555-1212
            [1] => 1-800-555-1212
        )
)

Example 2 – Extracting HTML tags and their contents:

<?php
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
    echo "matched: " . $val[0] . "
";
    echo "part 1: " . $val[1] . "
";
    echo "part 2: " . $val[2] . "
";
    echo "part 3: " . $val[3] . "
";
}
?>

Output (simplified):

matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>

Example 3 – Using named sub‑patterns:

<?php
$str = <<<FOO
a: 1
b: 2
c: 3
FOO;
preg_match_all('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
print_r($matches);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => a: 1
            [1] => b: 2
            [2] => c: 3
        )
    [name] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )
    [digit] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

regexpattern-matchingbackend-developmentpreg_match_all
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.