preg_replace_callback
Description
The preg_replace_callback of PCRE for PHP perform a regular expression search and replace using a callback.
Syntax
preg_replace_callback( string|array $pattern, callable $callback, string|array $subject, int $limit = -1, int &$count = null, int $flags = 0 ): string|array|null
Parameters
pattern
The pattern to search for. It can be either a string or an array with strings.
callback
A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string.
handler( array $matches ): string
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 callback subject
<? function myfunction($matches) { return date("Y") . "-" . $matches[1] . "-" . $matches[2]; } $pattern = "#(\d{2})/(\d{2})/(\d{4})#"; $callback = "myfunction"; $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($pattern, $callback, $subject); echo $return;
New Years Day: 2025-01-01 Christmas Eve: 2025-12-24 Christmas Day: 2025-12-25 New Years Eve: 2025-12-31
2 · limit
<? function myfunction($matches) { return date("Y") . "-" . $matches[1] . "-" . $matches[2]; } $pattern = "#(\d{2})/(\d{2})/(\d{4})#"; $callback = "myfunction"; $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($pattern, $callback, $subject, $limit); echo $return;
New Years Day: 2025-01-01 Christmas Eve: 2025-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"; $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($pattern, $callback, $subject, $limit, $count); echo $return . PHP_EOL; echo $count;
New Years Day: 2025-01-01 Christmas Eve: 2025-12-24 Christmas Day: 2025-12-25 New Years Eve: 2025-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"; $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($pattern, $callback, $subject, $limit, $count, $flags); echo $return;
New Years Day: 2025-01-01 (21 15 18) Christmas Eve: 2025-12-24 (47 41 44) Christmas Day: 2025-12-25 (73 67 70) New Years Eve: 2025-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"; $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($pattern, $callback, $subject, $limit, $count, $flags); echo $return;
NULL NULL '2001' '12' '24' '2001' '12' '25' '2001' NULL NULL '2001' New Years Day: 2025 Christmas Eve: 2025-12-24 Christmas Day: 2025-12-25 New Years Eve: 2025