Current Location: Home> Latest Articles> How to avoid unnecessary $matches content in preg_replace_callback_array using non-capture grouping?

How to avoid unnecessary $matches content in preg_replace_callback_array using non-capture grouping?

M66 2025-06-02

In PHP, the preg_replace_callback_array function is usually used to perform callback operations when performing regular replacement. This function accepts an array whose keys are regular expressions and values ​​are callback functions that handle matching. However, in some cases, we may not wish to capture certain groups to prevent them from appearing in $matches , resulting in unnecessary overhead or complex processing.

Use of non-capturing packets

To avoid receiving unnecessary $matches content in the callback function of preg_replace_callback_array , we can use non-capture grouping . Non-capturing grouping ensures that some groups are not captured when matched, thereby reducing unnecessary matching results in the callback function.

What is non-capturing grouping?

Non-capturing grouping is declared by prefixing parentheses in regular expressions with ?: . For example, (?:...) is a non-capturing group. In this case, groups in regular expressions are not captured into the $matches array, but are just components used to group match patterns.

Sample code

Suppose we have a regular expression with multiple groups inside, but we only care about the matching content of some groups, and not the content of others. Then, we can use non-capturing grouping to optimize our regular expressions.

 <?php
// Example:Replace the text inURL
$pattern = '/(?:https?:\/\/)(m66.net\/[a-z0-9\/\?=&]*)/i';
$replacement = function ($matches) {
    // Just careURLThe path part of,No need for the wholeURLand agreement section
    return 'https://m66.net' . $matches[1];
};

$input = 'Please visit https://m66.net/abc/def?query=xyz or http://m66.net/12345';
$output = preg_replace_callback_array([
    $pattern => $replacement,
], $input);

echo $output;
?>

Code parsing

  1. Regular expression :

    • (?:https?:\/\/) : This part is a non-capturing grouping, which matches the protocol part of the URL (http or https). Since we don't need this part, we use non-capture grouping.

    • (m66.net\/[a-z0-9\/\?=&]*) : This is the capture grouping, and what we care about is the path part behind m66.net (such as /abc/def?query=xyz ).

  2. Callback function :

    • In the callback function, we get the path part of the URL through $matches[1] , ignoring the protocol part ( https:// or http:// ).

    • Since the protocol part is not captured, preg_replace_callback_array will only provide $matches[1] as a replacement value to the callback function.

  3. Output result :

    • The URLs in the original text are correctly replaced with the format we care about, and there is no unnecessary $matches content.

Summarize

Using non-capturing grouping (?:...) can help us avoid receiving unnecessary matching groups in the callback function. This not only reduces unnecessary resource consumption, but also makes the code more concise and easy to understand, especially when dealing with complex regular expressions, it can effectively reduce the matching results that need attention.