Current Location: Home> Latest Articles> How to modify URL parameters with parse_url + http_build_query

How to modify URL parameters with parse_url + http_build_query

gitbox 2025-05-28

When developing web applications, the management of URL parameters is a very common requirement. You may need to add new parameters, modify existing parameters, or delete certain parameters. In PHP, parse_url() and http_build_query() are two very practical functions that can help you complete these tasks easily.

This article will introduce how to use parse_url() to decompose URLs, then use parse_str() and http_build_query() to modify parameters, and finally rebuild the complete URL.

1. Parsing URL

parse_url() can disassemble a URL into multiple parts, such as scheme (protocol), host (host), path (path), query (query string), etc. For example:

 $url = "https://gitbox.net/search?q=php&page=2";
$parts = parse_url($url);
print_r($parts);

Output:

 Array
(
    [scheme] => https
    [host] => gitbox.net
    [path] => /search
    [query] => q=php&page=2
)

2. Analyze query parameters

To modify the query parameters, you need to first parse the query part into an associative array. You can use parse_str() :

 parse_str($parts['query'], $queryParams);

At this point, $queryParams is:

 Array
(
    [q] => php
    [page] => 2
)

3. Modify parameters

You can operate parameters like operating a normal array:

 $queryParams['page'] = 3;         // Modify parameters
$queryParams['lang'] = 'zh';      // Add parameters
unset($queryParams['q']);         // Delete parameters

4. Rebuild the query string

Use http_build_query() to turn the parameter array back to a string:

 $newQuery = http_build_query($queryParams);

Then splice the new query string back to the original URL:

 $newUrl = $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $newQuery;
echo $newUrl;

Output:

 https://gitbox.net/search?page=3&lang=zh

V. Complete example

The following is a complete function that can pass in the original URL and the parameters to be modified, and return the modified URL:

 function modify_url_params($url, $modifications = [], $remove = []) {
    $parts = parse_url($url);

    parse_str($parts['query'] ?? '', $queryParams);

    // Apply modification
    foreach ($modifications as $key => $value) {
        $queryParams[$key] = $value;
    }

    // Delete parameters
    foreach ($remove as $key) {
        unset($queryParams[$key]);
    }

    $newQuery = http_build_query($queryParams);

    $newUrl = $parts['scheme'] . '://' . $parts['host'];
    if (isset($parts['path'])) {
        $newUrl .= $parts['path'];
    }
    if ($newQuery) {
        $newUrl .= '?' . $newQuery;
    }

    return $newUrl;
}

// Sample call
$originalUrl = "https://gitbox.net/search?q=php&page=2";
$newUrl = modify_url_params($originalUrl, ['page' => 1, 'lang' => 'zh'], ['q']);
echo $newUrl;

Output:

 https://gitbox.net/search?page=1&lang=zh

6. Summary

By combining parse_url() , parse_str() and http_build_query() , we can flexibly add, modify and delete URL query parameters. This method is not only clear, but also suitable for various practical scenarios, such as handling redirect links, paging logic, dynamic filtering conditions, etc. Mastering these techniques will greatly improve your efficiency and maintainability of your code when processing URLs.