preg_replace_callback_array
Description
The preg_replace_callback_array of PCRE for PHP perform a regular expression search and replace using callbacks.
Syntax
preg_replace_callback_array( array $pattern, string|array $subject, int $limit = -1, int &$count = null, int $flags = 0 ): string|array|null
Parameters
pattern
An associative array mapping patterns (keys) to callables (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.
Contant | Description |
---|---|
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
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 · pattern subject
<? function myfunction($matches) { return date("Y") . "-" . $matches[1] . "-" . $matches[2]; } $pattern = "#(\d{2})/(\d{2})/(\d{4})#"; $callback = "myfunction"; $pattern = [$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($pattern, $subject); echo $return; ?>
New Years Day: 2024-01-01 Christmas Eve: 2024-12-24 Christmas Day: 2024-12-25 New Years Eve: 2024-12-31
2 · limit
<? function myfunction($matches) { return date("Y") . "-" . $matches[1] . "-" . $matches[2]; } $pattern = "#(\d{2})/(\d{2})/(\d{4})#"; $callback = "myfunction"; $pattern = [$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($pattern, $subject, $limit); echo $return; ?>
New Years Day: 2024-01-01 Christmas Eve: 2024-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"; $pattern = [$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($pattern, $subject, $limit, $count); echo $return . PHP_EOL; echo $count; ?>
New Years Day: 2024-01-01 Christmas Eve: 2024-12-24 Christmas Day: 2024-12-25 New Years Eve: 2024-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"; $pattern = [$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($pattern, $subject, $limit, $count, $flags); echo $return; ?>
New Years Day: 2024-01-01 (21 15 18) Christmas Eve: 2024-12-24 (47 41 44) Christmas Day: 2024-12-25 (73 67 70) New Years Eve: 2024-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"; $pattern = [$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($pattern, $subject, $limit, $count, $flags); echo $return; ?>
NULL NULL '2001' '12' '24' '2001' '12' '25' '2001' NULL NULL '2001' New Years Day: 2024 Christmas Eve: 2024-12-24 Christmas Day: 2024-12-25 New Years Eve: 2024
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"; $pattern = [ $pattern1 => $callback1, $pattern2 => $callback1, $pattern3 => $callback2, $pattern4 => $callback2 ]; $subject = "AaaAaaAaaAaaBbbBbbBbbCccCccDdd"; $return = preg_replace_callback_array($pattern, $subject); echo $return; ?>
a: 12 b: 9 c: CccCcc d: Ddd