preg_replace_callback

FunctionParams and return types changed in PHP 8.0

Perform a regular expression search and replace using a callback.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

preg_replace_callback Function synopsis

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

Parameters

$pattern

Typearray|string

The pattern to search for. It can be either a string or an array with strings.

$callback

Typecallable

A callback that will be called and passed an array of matched elements in the $subject string. The callback should return the replacement string. This is the callback signature:

$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 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 Function

PHP 8.0

  • Return type added: array|string|null
  • Parameter name of parameter #1 changed: $regex to $pattern
  • Parameter type added for parameter #1 ($pattern): array|string
  • Parameter type added for parameter #2 ($callback): callable
  • Parameter type added for parameter #3 ($subject): array|string
  • Parameter type added for parameter #4 ($limit): int
  • Parameter default value added for position #4 ($limit): -1
  • Parameter default value added for position #5 ($count): null
  • Parameter type added for parameter #6 ($flags): int
  • Parameter default value added for position #6 ($flags): 0
  preg_replace_callback(
-     $regex,
+     array|string $pattern,
-     $callback,
+     callable $callback,
-     $subject,
+     array|string $subject,
-     $limit,
+     int $limit = -1,
-     &$count,
+     &$count = null,
-     $flags
+     int $flags = 0
-   )
+   ): array|string|null

PHP 7.4

  • Optional parameter added - #6: $flags
 preg_replace_callback(
     $regex,
     $callback,
     $subject,
     $limit,
-    &$count
+    &$count,
+    $flags
   )

preg_replace_callback 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 Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes