Current Location: Home> Latest Articles> Implement complex text processing using preg_replace_callback_array and array_map

Implement complex text processing using preg_replace_callback_array and array_map

M66 2025-06-02

In PHP, preg_replace_callback_array and array_map are two very powerful tools that can be used in combination to handle complex text operations. preg_replace_callback_array allows you to perform replacement operations based on a pattern set and can execute a callback function in each matching part. array_map can apply a callback function to each element of the array.

In this article, we will use an example to show how to use these two functions in combination to achieve complex text processing.

Scene description

Suppose we have a string containing multiple URLs and text, we want to do some processing on these URLs, such as replacing the domain name with m66.net , and custom replacements for some special tags in the string (such as [url] ). This is a common requirement, such as in crawlers or text cleaning scenarios.

Implementation steps

First, we need to define two main functions:

  1. Replace a specific pattern in a string with preg_replace_callback_array .

  2. Use array_map to perform further processing or conversion of the replaced results.

Let's take a look at the specific implementation.

 <?php

// 1. Define the callback function that needs to be replaced
$callback_array = [
    // match URL and replace the domain name
    '/https?:\/\/([\w\-]+\.[\w\-]+)/' => function($matches) {
        return 'https://' . 'm66.net';
    },
    // match [url] Tag and process
    '/\[url\](.*?)\[\/url\]/' => function($matches) {
        return '<a href="' . $matches[1] . '">' . $matches[1] . '</a>';
    }
];

// 2. Original text
$text = "This is a test string,Includes some URL: https://example.com and another link:[url]https://example2.com[/url]。";

// 3. use preg_replace_callback_array Make a replacement
$processed_text = preg_replace_callback_array($callback_array, $text);

// 4. use array_map Processing the array(This example simply converts text to capitalization)
$processed_text_array = array_map('strtoupper', explode(' ', $processed_text));

// 5. Merge the processed text into a string
$final_text = implode(' ', $processed_text_array);

// Output the final result
echo $final_text;
?>

Code parsing

  1. preg_replace_callback_array : This function accepts an associative array as the first parameter, the key of each array is a regular expression, and the value is a callback function. This callback function performs processing on the matching text.

    • In this example, we have two patterns:

      • URL replacement : match the URL starting with https:// or http:// via regular expression and replace its domain name with m66.net .

      • URL Tag Replacement : Match the URL between the [url] and [/url] tags and convert it to HTML hyperlink format.

  2. array_map : This function applies the specified callback function to each element of the array. Here we simply split the processed text into words by exploit and use array_map to convert each word to capitalization. Finally, merge them back into a string via implode .

Output result

Assume that the input text is:

 This is a test string,Includes some URL: https://example.com and another link:[url]https://example2.com[/url]。

After processing by preg_replace_callback_array and array_map , the output result will be:

 This is one test String,Include Some URL: https://m66.net and 另one Link:<a href="https://example2.com">https://example2.com</a>。

Summarize

Using preg_replace_callback_array and array_map , we are able to implement very complex text processing, especially when dealing with URLs or tag formats. This method is not only effective, but also flexible, and can expand different processing logic according to requirements. This approach works well if you need to process a lot of text.

Hopefully, through this simple example, you can better understand how these two functions are used and apply them in actual development.