Easy Tutorial
❮ Func Date Isodate Set Php Ref Timezones ❯

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+ versions.

Syntax

mixed preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count ]] )

The function is similar to preg_replace_callback(), but it uses a callback function for each pattern match.

Parameter Descriptions:

Return Value

Returns an array if subject is an array, otherwise returns a string. Returns NULL on error.

If a match is found, returns the modified target string (or array of strings); otherwise, returns the subject unchanged.

Example

Example 1

<?php
$subject = 'Aaaaaa Bbb';

preg_replace_callback_array(
    [
        '~[a]+~i' => function ($match) {
            echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
        },
        '~[b]+~i' => function ($match) {
            echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
        }
    ],
    $subject
);
?>

Execution result is as follows:

6 matches for "a" found
3 matches for "b" found

PHP Regular Expressions (PCRE)

❮ Func Date Isodate Set Php Ref Timezones ❯