Current Location: Home> Latest Articles> Use preg_replace_callback_array to handle multiple modes simultaneously

Use preg_replace_callback_array to handle multiple modes simultaneously

M66 2025-05-17

When dealing with string replacement, PHP's preg_replace_callback() function can flexibly process the matching results through a callback function. But if you have multiple different regular expressions, each need to be processed with different logic, preg_replace_callback_array() is your ideal choice.

Since PHP 7, preg_replace_callback_array() provides an elegant way to define multiple regular patterns and their corresponding callback functions in the form of an associative array.

This article will take you through its usage and show you how to handle multiple matching logic simultaneously with examples.

Basic syntax

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

  • $subject is the string to be processed.

Example: Replace link and email address at the same time

Suppose we want to identify the link and email address in the text, wrap the link into a <a> tag, and add mailto: to the email address.

 $text = <<<EOT
Welcome to our website http://m66.net/page,You can also send an email to support@m66.net consult。
EOT;

$result = preg_replace_callback_array([
    // deal with URL
    '/\bhttps?:\/\/[^\s]+/i' => function ($matches) {
        $url = htmlspecialchars($matches[0]);
        return "<a href=\"{$url}\">{$url}</a>";
    },
    // deal with邮箱地址
    '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i' => function ($matches) {
        $email = htmlspecialchars($matches[0]);
        return "<a href=\"mailto:{$email}\">{$email}</a>";
    }
], $text);

echo $result;

Output result:

 Welcome to our website <a href="http://m66.net/page">http://m66.net/page</a>,You can also send an email to <a href="mailto:support@m66.net">support@m66.net</a> consult。

Use scenarios

preg_replace_callback_array() is particularly suitable for handling:

  • Various text analysis, such as Markdown, BBCode, custom syntax, etc.;

  • Identification and replacement of multiple formats;

  • Improve readability and maintenance when processing different structures in complex text.

Tips

  1. The order of regular expressions is important, and the previous rules will be executed first.

  2. Use htmlspecialchars() to avoid XSS attacks (especially when processing user input).

  3. PHP's PCRE library is more powerful in higher versions, and it is recommended to use PHP 7.1+.

Summarize

Using preg_replace_callback_array() , we can easily define multiple regular patterns and their corresponding processing methods, with clearer code structure and more logical separation. It is a powerful and underrated tool in text processing.

Mastering it will not only make your code more elegant, but also significantly improve development efficiency.