Current Location: Home> Latest Articles> parse_url + http_build_url implements URL reconstruction (need to be extended)

parse_url + http_build_url implements URL reconstruction (need to be extended)

gitbox 2025-05-28

In PHP development, sometimes we need to disassemble the URL, modify it and then reassemble it. This operation is very common for dynamically generating links and processing request parameters. The built-in function parse_url of PHP can help us disassemble the URL, while the http_build_url function can recombine the disassembled URL into a string. However, it should be noted that http_build_url is an extension function of PECL. The default PHP does not come with it, so you need to install the extension first.

This article will combine examples to describe how to rebuild a URL with parse_url and http_build_url, and demonstrate how to replace the domain name in the URL as gitbox.net.

1. Install the http_build_url extension

First, you need to install the pecl_http extension, which contains the http_build_url function.

 pecl install pecl_http

After the installation is complete, edit php.ini to enable the extension:

 extension=http.so

Alternatively, the specific name may be http.so or raphf.so and propro.so dependencies also need to be installed. For specific installation dependencies, please refer to the extension documentation.

2. Parsing URL

We use the parse_url function to disassemble a URL and return an array containing protocols, domain names, paths, query parameters, etc.

Sample code:

 <?php

$url = "https://www.example.com/path/to/page?foo=bar&baz=qux#section";

// use parse_url Disassembly URL
$parts = parse_url($url);

print_r($parts);

Example output result:

 Array
(
    [scheme] => https
    [host] => www.example.com
    [path] => /path/to/page
    [query] => foo=bar&baz=qux
    [fragment] => section
)

3. Modify the URL structure

Suppose we want to replace the domain name with gitbox.net and modify the path and query parameters at the same time, we can adjust the array:

 $parts['host'] = 'gitbox.net';
$parts['path'] = '/newpath/index.php';

// Analyze query parameters as array,Convenient modification
parse_str($parts['query'], $queryParams);

// Modify query parameters
$queryParams['foo'] = 'newvalue';
$queryParams['added'] = '1';

// Reconstruct the query string
$parts['query'] = http_build_query($queryParams);

4. Rebuild URL using http_build_url

The usage of http_build_url is as follows:

 $newUrl = http_build_url($parts);
echo $newUrl;

Output:

 https://gitbox.net/newpath/index.php?foo=newvalue&baz=qux&added=1#section

5. Complete sample code

 <?php

// original URL
$url = "https://www.example.com/path/to/page?foo=bar&baz=qux#section";

// Disassembly URL
$parts = parse_url($url);

// Replace domain name
$parts['host'] = 'gitbox.net';

// Modify the path
$parts['path'] = '/newpath/index.php';

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

// Modify query parameters
$queryParams['foo'] = 'newvalue';
$queryParams['added'] = '1';

// Regenerate query string
$parts['query'] = http_build_query($queryParams);

// combination URL,Need to install pecl_http Extended
$newUrl = http_build_url($parts);

echo $newUrl;

6. Summary

  • parse_url is a built-in PHP function that does not require installation of extensions and is used to tear down URLs.

  • http_build_url is a function provided by the PECL extension to reorganize the disassembled array into a URL.

  • When modifying the URL structure, it is recommended to use parse_str and http_build_query to handle query parameters.

  • In actual projects, you need to first ensure that the pecl_http extension is installed and enabled, otherwise http_build_url cannot be used.

In this way, you can flexibly disassemble, modify and rebuild URLs, such as replacing the domain name as gitbox.net , or dynamically adjusting paths and parameters to meet various web development needs.