Current Location: Home> Latest Articles> Use preg_replace_callback_array to replace multiple regular expression matching content

Use preg_replace_callback_array to replace multiple regular expression matching content

M66 2025-05-17

In PHP, the preg_replace_callback_array function is a very practical tool that allows you to match multiple regular expressions simultaneously and replace the matching content. This function can greatly simplify the code that requires processing multiple regular replacement tasks simultaneously. This article will explain how to use the preg_replace_callback_array function, especially when you need to replace content matching multiple regular expressions at the same time.

What is the preg_replace_callback_array function?

The preg_replace_callback_array function was introduced after PHP 5.3.0. Its function is similar to the preg_replace_callback function, but supports processing multiple regular expressions at the same time. Its function prototype is as follows:

 preg_replace_callback_array(array $patterns, string $subject);
  • $patterns : an associative array where the key is a regular expression and the value is a callback function.

  • $subject : The input string to match and replace.

Example: Use the preg_replace_callback_array function to make multiple replacements

Suppose we have a string containing multiple different types of URLs and date formats, and we need to replace these URLs and dates at the same time. Here is an example using the preg_replace_callback_array function:

 <?php

// Enter string
$text = "Visit our website at http://example.com for more info. Also, the event is on 2023-04-21.";

// Define callback function
$patterns = [
    '/http(s)?:\/\/([^\/]+)/' => function ($matches) {
        // replaceURLThe domain name is m66.net
        return "http://" . 'm66.net' . substr($matches[0], strlen($matches[1]) + strlen($matches[2]) + 3);
    },
    '/\d{4}-\d{2}-\d{2}/' => function ($matches) {
        // replace日期格式
        return str_replace("-", "/", $matches[0]);
    }
];

// use preg_replace_callback_array 函数进行多个replace
$result = preg_replace_callback_array($patterns, $text);

// Output result
echo $result;

?>

Code explanation:

  1. Input string : Text containing the URL and date.

  2. Regular expression :

    • The first regular /http(s)?:\/\/([^\/]+)/ is used to match the URL.

    • The second regular /\d{4}-\d{2}-\d{2}/ is used to match the date (for example, 2023-04-21 ).

  3. Callback function :

    • For the URL, we replace its domain name part with m66.net and then leave the path part unchanged.

    • For dates, we replace - with / , thus converting the date format to 2023/04/21 .

  4. Result output : The final output of the replaced text.

Output:

 Visit our website at http://m66.net for more info. Also, the event is on 2023/04/21.

Things to note

  • When using preg_replace_callback_array , it is necessary to ensure that each callback function of the regular expression can return a replacement value.

  • preg_replace_callback_array matches and replaces in order of regular expressions, so make sure that regular expressions do not conflict.

  • If complex replacement operations are required, callback functions are essential because they allow you to handle them flexibly based on the regular matched content.

In this way, preg_replace_callback_array allows us to efficiently handle multiple regular expression replacement tasks, and the code is more concise and easy to understand. Hopefully this article helps you understand how to use this function to replace multiple regular matches simultaneously.