preg_replace_callback_array

Perform a regular expression search and replace using callbacks

Syntax

preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count [, int $flags = 0 ]]] ) : mixed

Parameters

patterns_and_callbacks

An associative array mapping patterns (keys) to callbacks (values).

subject

The string or an array with strings to search and replace.

limit

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

count

If specified, this variable will be filled with the number of replacements done.

flags

flags can be a combination of the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array.

PREG_OFFSET_CAPTURE For every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array 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 Unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string.

Return

preg_replace_callback_array() returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is NULL.

If matches are found, the new subject will be returned, otherwise subject will be returned unchanged.

Examples

1 · patterns_and_callbacks subject

<?

function myfunction($matches)
{
    return date("Y") . "-" . $matches[1] . "-" . $matches[2];
}

$pattern = "#(\d{2})/(\d{2})/(\d{4})#";
$callback = "myfunction";
$patterns_and_callbacks = array($pattern => $callback);
$subject = "New Years Day: 01/01/2001\n";
$subject .= "Christmas Eve: 12/24/2001\n";
$subject .= "Christmas Day: 12/25/2001\n";
$subject .= "New Years Eve: 12/31/2001";

$return = preg_replace_callback_array($patterns_and_callbacks, $subject);

echo $return;

?>
New Years Day: 2023-01-01
Christmas Eve: 2023-12-24
Christmas Day: 2023-12-25
New Years Eve: 2023-12-31

2 · limit

<?

function myfunction($matches)
{
    return date("Y") . "-" . $matches[1] . "-" . $matches[2];
}

$pattern = "#(\d{2})/(\d{2})/(\d{4})#";
$callback = "myfunction";
$patterns_and_callbacks = array($pattern => $callback);
$subject = "New Years Day: 01/01/2001\n";
$subject .= "Christmas Eve: 12/24/2001\n";
$subject .= "Christmas Day: 12/25/2001\n";
$subject .= "New Years Eve: 12/31/2001";
$limit = 2;

$return = preg_replace_callback_array($patterns_and_callbacks, $subject, $limit);

echo $return;

?>
New Years Day: 2023-01-01
Christmas Eve: 2023-12-24
Christmas Day: 12/25/2001
New Years Eve: 12/31/2001

3 · count

<?

function myfunction($matches)
{
    return date("Y") . "-" . $matches[1] . "-" . $matches[2];
}

$pattern = "#(\d{2})/(\d{2})/(\d{4})#";
$callback = "myfunction";
$patterns_and_callbacks = array($pattern => $callback);
$subject = "New Years Day: 01/01/2001\n";
$subject .= "Christmas Eve: 12/24/2001\n";
$subject .= "Christmas Day: 12/25/2001\n";
$subject .= "New Years Eve: 12/31/2001";
$limit = -1;

$return = preg_replace_callback_array($patterns_and_callbacks, $subject, $limit, $count);

echo $return . PHP_EOL;
echo $count;

?>
New Years Day: 2023-01-01
Christmas Eve: 2023-12-24
Christmas Day: 2023-12-25
New Years Eve: 2023-12-31
4

4 · flags · PREG_OFFSET_CAPTURE

<?

function myfunction($matches)
{
    return date("Y") . "-" . $matches[1][0] . "-" . $matches[2][0] . " (" . $matches[3][1] . " " . $matches[1][1] . " " . $matches[2][1] . ")";
}

$pattern = "#(\d{2})/(\d{2})/(\d{4})#";
$callback = "myfunction";
$patterns_and_callbacks = array($pattern => $callback);
$subject = "New Years Day: 01/01/2001\n";
$subject .= "Christmas Eve: 12/24/2001\n";
$subject .= "Christmas Day: 12/25/2001\n";
$subject .= "New Years Eve: 12/31/2001";
$limit = -1;
$flags = PREG_OFFSET_CAPTURE;

$return = preg_replace_callback_array($patterns_and_callbacks, $subject, $limit, $count, $flags);

echo $return;

?>
New Years Day: 2023-01-01 (21 15 18)
Christmas Eve: 2023-12-24 (47 41 44)
Christmas Day: 2023-12-25 (73 67 70)
New Years Eve: 2023-12-31 (99 93 96)

5 · flags · PREG_UNMATCHED_AS_NULL

<?

function myfunction($matches)
{
    var_export($matches[1]);
    echo " ";
    var_export($matches[2]);
    echo " ";
    var_export($matches[3]);
    echo PHP_EOL;
    
    $return = date("Y");
    
    if ($matches[1]) {
        $return .= "-" . $matches[1];
    }
    if ($matches[2]) {
        $return .= "-" . $matches[2];
    }

    return $return;
}

$pattern = "#(\d{2})?/?(\d{2})?/?(\d{4})#";
$callback = "myfunction";
$patterns_and_callbacks = array($pattern => $callback);
$subject = "New Years Day: 2001\n";
$subject .= "Christmas Eve: 12/24/2001\n";
$subject .= "Christmas Day: 12/25/2001\n";
$subject .= "New Years Eve: 2001";
$limit = -1;
$flags = PREG_UNMATCHED_AS_NULL;

$return = preg_replace_callback_array($patterns_and_callbacks, $subject, $limit, $count, $flags);

echo $return;

?>
NULL NULL '2001'
'12' '24' '2001'
'12' '25' '2001'
NULL NULL '2001'
New Years Day: 2023
Christmas Eve: 2023-12-24
Christmas Day: 2023-12-25
New Years Eve: 2023

6 · Multiple

<?

function myfunction1($matches)
{
    return $matches[0][1] . ": " . strlen($matches[0]) . PHP_EOL;
}
function myfunction2($matches)
{
    return $matches[0][1] . ": " . $matches[0] . PHP_EOL;
}

$pattern1 = "#[a]+#i";
$pattern2 = "#[b]+#i";
$pattern3 = "#[c]+#i";
$pattern4 = "#[d]+#i";
$callback1 = "myfunction1";
$callback2 = "myfunction2";
$patterns_and_callbacks = array($pattern1 => $callback1, $pattern2 => $callback1, $pattern3 => $callback2, $pattern4 => $callback2);
$subject = "AaaAaaAaaAaa BbbBbbBbb CccCcc Ddd";

$return = preg_replace_callback_array($patterns_and_callbacks, $subject);

echo $return;

?>
a: 12
 b: 9
 c: CccCcc
 d: Ddd

preg_filter

preg_grep

preg_last_error

preg_match

preg_match_all

preg_quote

preg_replace

preg_replace_callback

preg_split

PHP

Home Menu