What are some common methods for extracting specific parts of a URL in PHP?

When working with URLs in PHP, it is common to need to extract specific parts such as the domain, path, query parameters, or fragments. This can be done using various built-in functions and methods in PHP such as parse_url() for breaking down a URL into its components, $_SERVER['REQUEST_URI'] for getting the path of the current URL, and parse_str() for parsing query parameters.

// Extracting domain from a URL
$url = "https://www.example.com/path/to/page";
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;

// Extracting path from a URL
$path = parse_url($url, PHP_URL_PATH);
echo $path;

// Extracting query parameters from a URL
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
print_r($params);