Current Location: Home> Latest Articles> Use preg_replace_callback_array to implement tag-driven data injection

Use preg_replace_callback_array to implement tag-driven data injection

M66 2025-06-02

In PHP development, string replacement is a very common operation. When we need to replace a piece of text in different ways according to a set of different markers, preg_replace_callback_array is particularly convenient.

This article will show you how to implement a "mark-driven" data injection mechanism using preg_replace_callback_array , allowing you to flexibly embed data into template strings.

What is preg_replace_callback_array ?

preg_replace_callback_array is a function introduced in PHP 7. It allows you to replace strings through a set of regular expressions and corresponding callback functions.

Its signature is as follows:

 preg_replace_callback_array(array $patterns_and_callbacks, string $subject, int $limit = -1, int &$count = null): string

Application scenario: Tag-driven data injection

Imagine a situation where we have a paragraph of text that contains multiple different types of tags, for example:

 Welcome to visit[[url:/welcome]],The current time is{{time}}。

We want to replace [[url:/welcome]] with a hyperlink and {{time}} with the current time.

Step 1: Define regular and callback functions

We use two regular expressions to match the tags:

  • \[\[url:(.*?)\]\] is used to match the tag of URL type.

  • \{\{time\}\} is used to match time stamps.

 $template = 'Welcome to visit[[url:/welcome]],The current time is{{time}}。';

$patterns = [
    '/\[\[url:(.*?)\]\]/' => function ($matches) {
        $path = $matches[1];
        return '<a href="https://m66.net' . htmlspecialchars($path) . '">Click here</a>';
    },
    '/\{\{time\}\}/' => function () {
        return date('Y-m-d H:i:s');
    },
];

$output = preg_replace_callback_array($patterns, $template);

echo $output;

Output result:

 Welcome to visit<a href="https://m66.net/welcome">Click here</a>,The current time is2025-04-21 14:30:00。

Note: The actual time will vary according to the time you run.

Why use preg_replace_callback_array ?

This approach has several advantages over traditional multiple preg_replace_callback or string replacement:

  1. Clear structure : All patterns and replacement logic are concentrated in one array, easy to maintain.

  2. More efficient execution : All modes can be processed at once without multiple scans of text.

  3. Strong scalability : Adding a new tag only requires adding an item to the array, and there is almost no need to modify other code.

Extension: Support for more tags

If we want to support more dynamic tags, such as user names, random numbers, etc., just continue to expand the $patterns array:

 $patterns['/\{\{user\}\}/'] = function () {
    return 'Xiao Ming';
};

$patterns['/\{\{rand\}\}/'] = function () {
    return rand(1, 100);
};

Then use it in the template like this:

 Welcome,{{user}}!Your lucky number is:{{rand}}。

Summarize

A tag-driven data injection engine can be easily implemented using preg_replace_callback_array . It is not only semantic, but also highly extensible, and is very suitable for use in template systems, content processing, rich text escaping and other scenarios.

Hope this article will be helpful for you to understand and use this function. If you are developing a lightweight template engine or content rendering tool, consider this approach to improve the quality and flexibility of your code.