How can the explode() function be utilized to separate parts of a URL in PHP?
To separate parts of a URL in PHP, you can use the explode() function to split the URL string into an array based on a delimiter, such as "/". This allows you to access specific parts of the URL, such as the domain, path, or query parameters, by indexing the array elements.
$url = "https://www.example.com/path/to/page?param1=value1&param2=value2";
$parts = explode("/", $url); // Split URL by "/"
$domain = $parts[2]; // Get domain
$path = explode("?", $parts[3])[0]; // Get path
$queryString = explode("?", $parts[3])[1]; // Get query string
echo "Domain: " . $domain . "<br>";
echo "Path: " . $path . "<br>";
echo "Query String: " . $queryString . "<br>";