What is the purpose of the parse_url function in PHP?

The parse_url function in PHP is used to parse a URL string and return its components such as scheme, host, path, query parameters, etc. This function is useful when working with URLs in PHP to extract specific parts of the URL for further processing or manipulation.

$url = "https://www.example.com/path/to/page?param1=value1&param2=value2";
$url_components = parse_url($url);

echo "Scheme: " . $url_components['scheme'] . "\n";
echo "Host: " . $url_components['host'] . "\n";
echo "Path: " . $url_components['path'] . "\n";

$query_params = [];
parse_str($url_components['query'], $query_params);
echo "Query Parameters: \n";
print_r($query_params);