PHP.nl

preg_replace_callback_array

preg_replace_callback_array

Perform a regular expression search and replace using callbacks

 **preg_replace_callback_array** array $pattern  $subject int $limit int $count int $flags

The behavior of this function is similar to , except that callbacks are executed on a per-pattern basis. preg_replace_callback

pattern An associative array mapping patterns (keys) to s (values). callable

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

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

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

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

returns an array if the
parameter is an array, or a string

otherwise. On errors the return value is null preg_replace_callback_array``subject

If matches are found, the new subject will be returned, otherwise will be returned unchanged. subject

Voorbeeld: example

<?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
);
?>
6 matches for "a" found
3 matches for "b" found

PCRE Patternspreg_replace_callback``preg_quote``preg_replace``preg_last_errorAnonymous functions