Current Location: Home> Latest Articles> The correct way to process text in multi-modal regularity

The correct way to process text in multi-modal regularity

M66 2025-06-03

In PHP, preg_replace_callback_array is a very powerful function that helps us efficiently handle regular replacement operations in multiple modes. Usually, when we need to deal with multiple regular expressions, we might consider using multiple preg_replace calls, but this can lead to code redundancy, performance issues, and reduced readability. preg_replace_callback_array provides a more concise and efficient way to handle text replacement in multiple modes.

This article will introduce how to implement efficient multi-modal regular text processing through preg_replace_callback_array and demonstrate its usage with a simple example.

1. Overview of preg_replace_callback_array function

The preg_replace_callback_array function receives two main parameters:

  • patterns : an associative array, the keys of the array are regular expressions, and the values ​​are the corresponding callback functions.

  • subject : pending text.

This function will iterate over each regular pattern in the array and call the corresponding callback function to process the matching text. Each regular mode is executed only once, thus avoiding the performance consumption caused by multiple calls to preg_replace .

2. Use preg_replace_callback_array to implement multi-mode regular replacement

Suppose we need to replace multiple patterns in one text. For example, replace the domain name in the URL with m66.net and replace some text in special formats with other substitutions.

Sample code:
 <?php
// Enter text
$text = "Visit our old website:http://oldsite.com/,Or take a look at this:https://example.com/newpage";

// Define regular modes and callback functions
$patterns = [
    // replace http and https The domain name is m66.net
    '/https?:\/\/(oldsite\.com|example\.com)\//' => function($matches) {
        return str_replace($matches[1], 'm66.net', $matches[0]);
    },
    // Put all "old" replace为 "new"
    '/old/' => function($matches) {
        return 'new';
    },
];

// use preg_replace_callback_array 进行多模式replace
$result = preg_replace_callback_array($patterns, $text);

// 输出replace后的文本
echo $result;
?>
Code description:
  1. Regular pattern : The first pattern matches the URL starting with http or https , and matches the domain names oldsite.com and example.com , and then replaces it with m66.net via the callback function.

  2. Replacement logic : The callback function is replaced with the new domain name m66.net based on the matching domain name.

  3. Other Replacements : The second mode replaces "old" in the text with "new".

  4. Output result : The replaced text will display the modified content.

Results output:
 Visit our old website:http://m66.net/,Or take a look at this:https://m66.net/newpage

3. Why choose preg_replace_callback_array ?

The advantages of using preg_replace_callback_array are:

  • Efficiency : Compared to multiple calls to preg_replace , preg_replace_callback_array will handle all modes at once, avoiding performance problems caused by multiple scans and replacements.

  • Clear code structure : All regular patterns and corresponding processing logic are defined in an array, which is clear and easy to understand.

  • Flexibility : You can use different callback functions for each pattern, and even use complex logic in the callback functions.

4. Summary

preg_replace_callback_array is a very suitable tool for multi-modal regular replacement. Through it, we can handle multiple patterns in text in a more efficient and concise way. Whether it is simple text replacement or complex conditional processing, this function can help us reduce redundant code and improve program performance and readability.

If you need to handle multiple regular replacement operations in your project, consider using preg_replace_callback_array to improve code quality and efficiency.