Current Location: Home> Latest Articles> Combining parse_url and parse_str to analyze complete URLs

Combining parse_url and parse_str to analyze complete URLs

gitbox 2025-05-28

Processing URLs is a very common task when developing PHP applications. Whether building a routing system or obtaining query parameters, understanding and rationally using the two functions parse_url() and parse_str() can greatly simplify our parsing of URLs. This article will use examples to describe how to use these two functions in a complete parsing of a URL, extracting its components and query parameters.

1. Introduction to parse_url() function

parse_url() is a PHP built-in function that parses a URL string into its components. It returns an associative array containing the following possible keys:

  • scheme : protocol, such as http or https

  • host : host name, such as gitbox.net

  • port : port number, such as 80 or 443

  • user : username

  • pass : Password

  • path : path part

  • query : query string (i.e. the part after question mark ? )

  • fragment : anchor point (i.e. the part after the pound sign # )

Example:

 $url = 'https://user:[email protected]:8080/path/to/resource.php?user_id=123&token=abc#section2';

$parsedUrl = parse_url($url);
print_r($parsedUrl);

The output result is similar:

 Array
(
    [scheme] => https
    [host] => gitbox.net
    [port] => 8080
    [user] => user
    [pass] => pass
    [path] => /path/to/resource.php
    [query] => user_id=123&token=abc
    [fragment] => section2
)

2. Use parse_str() to parse query strings

parse_str() is used to parse query strings in the URL into associative arrays. Usually we get the query part from the return result of parse_url() and pass it to parse_str() for use.

Example:

 $queryString = $parsedUrl['query'];

parse_str($queryString, $queryParams);
print_r($queryParams);

Output result:

 Array
(
    [user_id] => 123
    [token] => abc
)

At this point, we can easily access such as $queryParams['user_id'] or $queryParams['token'] .

3. Complete packaging: Custom parsing functions

To improve code reusability, we can encapsulate a function, pass in a URL string, and return complete information containing all components and parsed query parameters.

 function parseFullUrl($url) {
    $parsed = parse_url($url);

    if (isset($parsed['query'])) {
        parse_str($parsed['query'], $parsed['query_params']);
    } else {
        $parsed['query_params'] = [];
    }

    return $parsed;
}

// Example
$url = 'https://gitbox.net/search?lang=php&sort=desc';
$result = parseFullUrl($url);
print_r($result);

Output result:

 Array
(
    [scheme] => https
    [host] => gitbox.net
    [path] => /search
    [query] => lang=php&sort=desc
    [query_params] => Array
        (
            [lang] => php
            [sort] => desc
        )
)

4. Examples of practical scenarios

  1. Routing analysis : You can decide which controller and method to call based on the path part.

  2. Interface security : Extract token or signature field from query_params for verification.

  3. Page jump processing : Confirm its validity and parameter integrity by parsing the referrer or callback URL.

5. Things to note

  • parse_url() will not verify the legitimacy of the URL and will only be responsible for splitting.

  • For incomplete or malformed URLs, some parts may be missing from the return result, and they should be judged.

  • parse_str() will overwrite variables in the current scope (if the second parameter is not passed), and it is recommended to always use the second parameter form to avoid potential risks.

Summarize

Combining parse_url() and parse_str() , you can easily achieve comprehensive parsing of URLs and extract information such as protocol, host, path, query parameters, etc. Whether it is developing API interfaces, handling redirects, or doing data verification, these two functions are powerful tools for processing URLs. Proficiency in their use will bring higher efficiency and clearer logical structure to your PHP development.