PHP preg_replace_callback_array() Function
PHP Regular Expressions (PCRE)
The preg_replace_callback_array() function performs a regular expression search and replaces using a callback.
This function is supported in PHP 7+.
Syntax
mixed preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count ]] )
The function is similar topreg_replace_callback(), but it replaces by invoking the callback function based on each pattern match.
Parameter Description:
$patterns_and_callbacks: associative array, key (pattern) => value (callback function)
$subject: The string or array to search and replace.
$limit: Optional, the maximum number of replacements for each pattern, defaults to -1 (no limit, replace all matches).
$count: Optional, specifies the number of replacements.
Return Value
If subject is an array, returns an array; otherwise returns a string. Returns NULL on error.
If a match is found, returns the replaced target string (or array of strings); otherwise subject will be returned unchanged.
Examples
Example 1
The execution result is as follows:
6 matches for "a" found 3 matches for "b" foundOther Extensions