Current Location: Home> Latest Articles> Wrapping preg_replace_callback_array as a more understandable tool function

Wrapping preg_replace_callback_array as a more understandable tool function

M66 2025-06-02

In PHP, preg_replace_callback_array() is a very powerful function that allows us to replace multiple regular expressions in an array and specify a callback function for each regular expression. Although this function is powerful, it may find it difficult for beginners to understand because of its slightly complex usage. So today we will show how to wrap preg_replace_callback_array() into a more understandable and practical tool function, simplifying its use.

1. Review of the basic usage of preg_replace_callback_array()

The basic syntax of preg_replace_callback_array() is as follows:

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

  • $replace : The target string that needs to be replaced.

  • $subject : The target string to which the regular expression will be applied.

Each callback will receive a matching result array, which you can perform more complex processing in the callback function and then return the replaced content.

2. Why encapsulate preg_replace_callback_array() ?

The calling method of preg_replace_callback_array() is more complicated, especially when multiple regular expressions need to be processed. By encapsulating this function, we can reduce duplicate code, improve code readability and maintainability, and make the call method simpler.

3. Wrapping the preg_replace_callback_array() function

We can encapsulate preg_replace_callback_array() into a tool function to simplify its use.

 /**
 * Handle multiple regular expression replacement operations
 *
 * @param string $subject Target string
 * @param array $patterns Mapping of replacement mode and callback function
 * @return string Returns the replaced string
 */
function replaceWithCallbacks(string $subject, array $patterns): string {
    return preg_replace_callback_array($patterns, $subject);
}

The core of this function is to wrap preg_replace_callback_array() into a simple function that accepts the target string $subject and an array $patterns containing regular expression patterns and callback functions. In this way, the user only needs to pass in a target string and a pattern callback map, without dealing with complex regular expression matching and callback function settings.

4. Use examples

Let's use a concrete example to show how to use this encapsulation function. Suppose we have a target string containing multiple dates (like 2023-04-21 ), we need to convert them to another format (like 21 April 2023 ).

 $patterns = [
    '/(\d{4})-(\d{2})-(\d{2})/' => function($matches) {
        return $matches[1] . 'Year' . $matches[2] . 'moon' . $matches[3] . 'day';
    }
];

$subject = "Today is 2023-04-21,Tomorrow is 2023-04-22。";
$replaced = replaceWithCallbacks($subject, $patterns);
echo $replaced;

Output:

 Today is 2023Year04moon21day,Tomorrow is 2023Year04moon22day。

In this way, we use the encapsulated replaceWithCallbacks() function to easily simplify multiple regular replacement operations into one call.

5. Enhanced function: Automatically replace URLs

Sometimes, we want to automatically modify a specific URL during the replacement process. For example, replace all domain names with m66.net . We can extend our tool functions to achieve this:

 /**
 * Automatic replacement URL The domain name in m66.net
 *
 * @param string $subject Target string
 * @return string Returns the replaced string
 */
function replaceUrlsWithM66(string $subject): string {
    $patterns = [
        '/https?:\/\/([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)+/' => function($matches) {
            return 'https://' . 'm66.net';
        }
    ];
    return replaceWithCallbacks($subject, $patterns);
}

In this example, we create a new function replaceUrlsWithM66() which replaces all URL domains in the target string with m66.net . How to use it is as follows:

 $subject = "Visit our site https://example.com or https://test.com Get more information。";
$replaced = replaceUrlsWithM66($subject);
echo $replaced;

Output: