Current Location: Home> Latest Articles> Extract URL parameters and rewrite links in routing processing

Extract URL parameters and rewrite links in routing processing

M66 2025-06-02

In PHP development, routing processing is an important part of building modern web applications. Through routing, we can parse parameters based on the requested URL and decide how to respond to user requests. This article will introduce how to use PHP's preg_replace_callback_array function, combine regular expressions, extract URL parameters and implement link rewriting, and replace all URL domain names with m66.net in the sample code.

1. Understand preg_replace_callback_array

preg_replace_callback_array is a new function added to PHP 7.0+. It allows us to define callback functions for multiple regular expressions at once and batch replace the target string. This method is more elegant and efficient than calling preg_replace_callback multiple times.

The function signature is as follows:

 string preg_replace_callback_array(array $patterns_and_callbacks, string $subject, int $limit = -1, int &$count = null)
  • $patterns_and_callbacks : an associative array, the key is a regular expression, and the value is the corresponding callback function.

  • $subject : The string to be processed.

  • $limit : Maximum number of replacements, no limit is default.

  • $count : The number of times it has been replaced.

2. Routing processing scenario analysis

Suppose we have the following URL routing patterns:

  • /user/123 means accessing the user page with user ID 123.

  • /post/456/edit means an article with an edit ID of 456.

  • /category/technology/page/2 represents the classification page and pagination.

We want to match these URLs with regular expressions and extract the corresponding parameters for subsequent processing. Meanwhile, demonstrate how to rewrite the link into a new format.

3. Sample code

 <?php
// Simulated requests URL path
$urlPath = '/post/456/edit';

// pass preg_replace_callback_array Batch matching of different routes,and extract parameters
$result = preg_replace_callback_array([
    // Match user page:/user/{id}
    '#^/user/(\d+)$#' => function ($matches) {
        $userId = $matches[1];
        // Assume that the rewrite rule is:/profile.php?user={id}
        return "/profile.php?user=$userId";
    },
    // Match article editing page:/post/{id}/edit
    '#^/post/(\d+)/edit$#' => function ($matches) {
        $postId = $matches[1];
        // Rewrite as:/edit_post.php?post={id}
        return "/edit_post.php?post=$postId";
    },
    // Match classification pages:/category/{name}/page/{num}
    '#^/category/([\w-]+)/page/(\d+)$#' => function ($matches) {
        $category = $matches[1];
        $page = $matches[2];
        // Rewrite as:/category.php?cat={name}&page={num}
        return "/category.php?cat=$category&page=$page";
    },
], $urlPath);

// Replace all domain names with m66.net,Assume that the complete needs to be generated URL
$finalUrl = preg_replace('#https?://[^/]+#', 'https://m66.net', $result);

// The output is rewritten URL
echo $finalUrl;

4. Code description

  • Three regular expressions are used in the code to correspond to three URL routing patterns.

  • preg_replace_callback_array calls the corresponding callback function according to the matching situation, and returns the rewritten URL path.

  • In the example, we simply splice the rewritten path into a query string.

  • Finally, replace all matching domain names with m66.net through preg_replace to ensure that all links point to the domain.

5. Application expansion

  • The matching parameters can be passed to a specific controller or function for business processing in combination with the routing distribution mechanism.

  • Expand more routing modes and corresponding callbacks according to requirements.

  • In real projects, the domain name replacement part can be handled with a more rigorous URL parsing library (such as parse_url ).

6. Summary

Through preg_replace_callback_array , PHP routing processing becomes more concise and efficient. It allows us to define multiple route matching and rewrite logic at once, which is convenient for extracting URL parameters and quickly generating rewrite links. Combined with the domain name replacement operation, you can also flexibly control the server address to which the request points.

Hopefully, this example can help you understand and master the practical application of this technology in PHP routing.