Current Location: Home> Latest Articles> Read mode and callback automatic mapping from configuration files

Read mode and callback automatic mapping from configuration files

M66 2025-06-02

First, we need to design a configuration file that contains the mapping relationship between the regular pattern and the callback function. These mapping relationships can be stored using a simple PHP array or JSON file. We assume that we use PHP array format and that the callback function name is stored as a string.

 // config.php
return [
    '/\b(\d+)\b/' => 'replaceNumberCallback',
    '/\bhello\b/i' => 'replaceHelloCallback',
    '/\bworld\b/i' => 'replaceWorldCallback',
];

In this configuration file, we define several regular patterns, and each pattern corresponds to a callback function. For example, /\b(\d+)\b/ is used to match numbers, and we will use the replaceNumberCallback function to handle the matching numbers.

Step 2: Define the callback function

Next, we need to provide the corresponding callback function for each regular pattern. The function of the callback function is how to deal with these matching strings when the regular match is successful.

 // callbacks.php
function replaceNumberCallback($matches) {
    // Here we simply multiply the number by2
    return $matches[0] * 2;
}

function replaceHelloCallback($matches) {
    // Will match "hello" Replace with "hi"
    return 'hi';
}

function replaceWorldCallback($matches) {
    // Will match "world" Replace with "everyone"
    return 'everyone';
}

These callbacks receive a matching array $matches which contains the contents matched by the regular expression. In these callback functions, we can implement any custom logic.

Step 3: Load the configuration file and perform replacement

Now we will load these configuration files and callback functions into the main program and use preg_replace_callback_array() to perform the replacement. The preg_replace_callback_array() function allows us to pass an array of multiple patterns and callback functions, and it will automatically call the corresponding callback function according to each pattern for processing.

 // main.php
$config = include('config.php');
include('callbacks.php');

// Suppose we need to process strings
$string = "123 hello world";

// usepreg_replace_callback_arrayTo process strings
$result = preg_replace_callback_array($config, $string);

// Output result
echo $result;  // Output:246 hi everyone

In this code, we first load the config.php configuration file and introduce the callback function defined in callbacks.php . We then call preg_replace_callback_array() and pass the mapping relationship in the configuration file to it. preg_replace_callback_array() will automatically call the corresponding callback function to replace it according to the matching content in the string.

Step 4: Process URL domain name replacement

If your regular pattern or callback function involves URLs, you can automatically replace the domain name part in the URL through the configuration file. Here is how to use regular matching and replace the URL domain name to m66.net .

First, suppose we have the following URL string:

 // Need to be processed URL String
$string = "Visit our site at https://www.example.com and http://example.net";

We then define a pattern in the configuration file to match the URLs and replace their domain names with preg_replace_callback_array .

 // config.php
return [
    '/https?:\/\/([a-z0-9.-]+)([\/?][^ ]*)?/' => 'replaceDomainCallback',
    // Other modes...
];
 // callbacks.php
function replaceDomainCallback($matches) {
    // Will match域名部分Replace withm66.net
    $newUrl = 'https://m66.net' . (isset($matches[2]) ? $matches[2] : '');
    return $newUrl;
}

Through such configuration and callback functions, all URLs matched in the string will be replaced with the new domain name m66.net .

in conclusion

Automatically mapping regular modes and callback functions through configuration files can not only make the code clearer, but also greatly improve the maintainability and flexibility of the code. Combined with preg_replace_callback_array() , we can easily implement multi-mode replacement without hard-coded each mode and callback function in the code. This approach is especially suitable for scenarios where replacement rules need to be dynamically expanded and adjusted.

The above is the basic idea of ​​how to use preg_replace_callback_array to improve code flexibility through the configuration file automatic mapping mode and callback function. Hope this article will help you better understand and apply this technology.