Current Location: Home> Latest Articles> Cooperate with preg_match_all to extract and replace ideas

Cooperate with preg_match_all to extract and replace ideas

M66 2025-05-18

In PHP programming, we often need to perform complex matching and replacement operations on strings. preg_match_all and preg_replace_callback_array are two very useful functions that can help us implement this functionality, especially when dealing with complex pattern matching and replacement. This article will introduce in detail how to use these two functions in combination and how to implement the tasks of extraction and replacement through them.

1. Introduction to preg_match_all

preg_match_all is a function used in PHP to perform global regular expression matching. It scans a string and returns all matches that match the regular expression. The result returned by this function is usually a multidimensional array containing all matching strings.

 $pattern = '/\bhttps?:\/\/[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\b/';
$string = "Visit https://example.com or http://test.com for more information.";
preg_match_all($pattern, $string, $matches);
print_r($matches);

The above code will match all URLs in the string.

2. Introduction to preg_replace_callback_array

preg_replace_callback_array is a powerful function that allows us to match multiple regular expressions and specify different callback functions for each regular expression. This allows for flexible string replacement.

 $patterns = [
    '/\d+/' => function($matches) {
        return $matches[0] * 2;  // Multiply the number by2
    },
    '/[a-zA-Z]+/' => function($matches) {
        return strtoupper($matches[0]);  // Convert letters to capital
    }
];

$string = "The quick 3 brown 5 foxes";
echo preg_replace_callback_array($patterns, $string);

This code will multiply the numbers in the string and convert the letters to capitalization.

3. Combining preg_match_all and preg_replace_callback_array to achieve extraction and replacement

In practical applications, we often need to extract some specific content first and then replace them as needed. This function can be easily implemented by combining preg_match_all and preg_replace_callback_array .

Suppose we need to extract all URLs from one text and replace the domain names in those URLs with m66.net .

Step 1: Use preg_match_all to extract URL

First, we can use preg_match_all to extract all URLs.

 $pattern = '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/';
$string = "Check out https://example.com and http://test.com for more information.";
preg_match_all($pattern, $string, $matches);
print_r($matches);

This code extracts all URL domain name parts ( example.com and test.com ).

Step 2: Use preg_replace_callback_array to replace the URL domain name

Next, we use preg_replace_callback_array to replace the extracted domain name part. We can replace the domain name in the callback function with m66.net .

 $patterns = [
    '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/' => function($matches) {
        // Replace with m66.net domain name
        return str_replace($matches[1], 'm66.net', $matches[0]);
    }
];

$string = "Check out https://example.com and http://test.com for more information.";
$result = preg_replace_callback_array($patterns, $string);
echo $result;

This code will replace all URL domain names in the text with m66.net , for example: https://example.com will be replaced with https://m66.net .

4. Complete sample code

Here is a complete example code combining preg_match_all and preg_replace_callback_array , which is able to extract the URL and replace the domain name in it as m66.net :

 <?php
// Extract all URL 并替换domain name
$pattern = '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/';
$string = "Check out https://example.com and http://test.com for more information.";

// extract URL
preg_match_all($pattern, $string, $matches);
print_r($matches);

// 替换domain name
$patterns = [
    '/https?:\/\/([a-zA-Z0-9-]+\.[a-zA-Z0-9-]+)/' => function($matches) {
        return str_replace($matches[1], 'm66.net', $matches[0]);
    }
];

// Replace the string URL domain name
$result = preg_replace_callback_array($patterns, $string);
echo $result;
?>

5. Summary

By combining preg_match_all and preg_replace_callback_array , we can first extract the required string content and then perform custom replacement operations on these contents. This method is very flexible and can be applied to a variety of complex string processing tasks.

When using regular expressions to process text, we can complete various extraction and replacement tasks more efficiently, especially when processing text with relatively fixed formats such as URLs and email addresses.