Current Location: Home> Latest Articles> Preg_replace_callback_array Basic usage details

Preg_replace_callback_array Basic usage details

M66 2025-06-03

In PHP, the processing of regular expressions is an indispensable part of daily development. To improve readability and flexibility, PHP 7 introduced a new function - preg_replace_callback_array . This function combines the advantages of regular matching and callback processing, and is a concise encapsulation of complex replacement logic.

This article will explain in-depth the basic usage and practical application scenarios of preg_replace_callback_array to help you process strings more efficiently.

1. What is preg_replace_callback_array?

preg_replace_callback_array is a new function added in PHP 7+. It allows us to define a mapping array of "regular expression => callback functions" and then apply all these regular rules and corresponding processing logic at once.

The syntax is as follows:

 preg_replace_callback_array(array $patterns_and_callbacks, string $subject[, int $limit = -1[, int &$count = null]])
  • $patterns_and_callbacks : an associative array, the key is a regular expression, and the value is the corresponding callback function.

  • $subject : The target string to be replaced.

  • $limit : Optional parameter, specifying the maximum number of replacements.

  • $count : Optional parameter to receive the total number of replacements.

2. Basic usage examples

Here is a simple example showing how to process a string using two regular expressions and corresponding callback functions:

 $text = 'Visit our official website:http://m66.net Or send an email to support@m66.net Get support。';

$result = preg_replace_callback_array([
    // matchURL
    '/https?:\/\/[^\s]+/i' => function ($matches) {
        return '<a href="' . $matches[0] . '">' . $matches[0] . '</a>';
    },
    // matchEmailaddress
    '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i' => function ($matches) {
        return '<a href="mailto:' . $matches[0] . '">' . $matches[0] . '</a>';
    }
], $text);

echo $result;

Output:

 Visit our official website:<a href="http://m66.net">http://m66.net</a> Or send an email to <a href="mailto:support@m66.net">support@m66.net</a> Get support。

3. Practical application scenarios

1. Uniform replacement of multiple modes

In the traditional preg_replace_callback , if we need to deal with multiple patterns, we often need to write multiple processing flows. Using preg_replace_callback_array , all logic can be centrally managed, with clear structure and easy to maintain.

2. Easily implement rich text parsing

For example, automatically converting URLs, emails, emoticons, etc. into HTML tags, this is the strength of preg_replace_callback_array .

 $text = 'See this link:https://m66.net,Send an email to hello@m66.net,And a smiling face :)';

$result = preg_replace_callback_array([
    '/https?:\/\/[^\s]+/' => function ($m) {
        return '<a href="' . $m[0] . '">' . $m[0] . '</a>';
    },
    '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i' => function ($m) {
        return '<a href="mailto:' . $m[0] . '">' . $m[0] . '</a>';
    },
    '/:\)/' => function () {
        return '??';
    }
], $text);

echo $result;

3. High-performance batch replacement

Compared with multiple calls to preg_replace_callback , preg_replace_callback_array completes all matching and replacement at once, reducing performance overhead, especially in large text or complex replacements.

4. Things to note

  • The PHP version must be 7.0 or higher.

  • The callback function of each regular expression should return the replacement result, otherwise it will cause the replacement to fail or an empty string.

  • The order of replacement depends on the order of definition of the array, so pay attention to the order of the array when there is a dependency order.

5. Summary

preg_replace_callback_array is a very practical function, especially suitable for multi-modal text parsing and replacement. By making rational use of this function, we can handle complex regular replacement tasks in a more concise and clear manner, improving the readability and maintainability of the code.

Whether it is a development forum, a rich text editor or a simple data cleaning, preg_replace_callback_array can play an important role.

What difficulties do you encounter in the process of using regularity? Welcome to leave a message for communication!