preg_replace_callback_array

FunctionParams and return types changed in PHP 8.0

Perform a regular expression search and replace using callbacks.

PHP 5
PHP 7.0
Added
PHP 7.1-7.4
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

preg_replace_callback_array Function synopsis

preg_replace_callback_array(
    array $pattern,
    array|string $subject,
    int $limit = -1,
    &$count = null,
    int $flags = 0
  ): array|string|null

Parameters

$pattern

Typearray

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

$subject

Typearray|string

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

$limit

OptionalTypeintDefault value-1

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

$count

Passed by reference, OptionalDefault valuenull

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

$flags

OptionalTypeintDefault value0

$flags can be a combination of the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array. See the description in preg_match for more details.

Note that the parameter $count is passed by reference, and contains additional/updated context data that may be useful.

Return value

Typearray|string|null

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.

Changes to the preg_replace_callback_array Function

PHP 8.0

  • Return type added: array|string|null
  • Parameter type added for parameter #1 ($pattern): array
  • Parameter type added for parameter #2 ($subject): array|string
  • Parameter type added for parameter #3 ($limit): int
  • Parameter default value added for position #3 ($limit): -1
  • Parameter default value added for position #4 ($count): null
  • Parameter type added for parameter #5 ($flags): int
  • Parameter default value added for position #5 ($flags): 0
  preg_replace_callback_array(
-     $pattern,
+     array $pattern,
-     $subject,
+     array|string $subject,
-     $limit,
+     int $limit = -1,
-     &$count,
+     &$count = null,
-     $flags
+     int $flags = 0
-   )
+   ): array|string|null

PHP 7.4

  • Optional parameter added - #5: $flags
 preg_replace_callback_array(
     $pattern,
     $subject,
     $limit,
-    &$count
+    &$count,
+    $flags
   )

PHP 7.0

  • Function added

preg_replace_callback_array Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported No
PHP 5.5Unsupported No
PHP 5.4Unsupported No
PHP 5.3Unsupported No