How can string manipulation functions in PHP be utilized to extract specific parts of a URL?

To extract specific parts of a URL in PHP, you can utilize string manipulation functions such as strpos, substr, and explode. These functions can help you locate and extract specific segments of a URL, such as the domain, path, query parameters, or fragment. By using a combination of these functions, you can effectively parse and extract the desired information from a URL.

$url = "https://www.example.com/path/to/page?param1=value1&param2=value2#section";

// Extract domain
$domain = parse_url($url, PHP_URL_HOST);

// Extract path
$path = parse_url($url, PHP_URL_PATH);

// Extract query parameters
parse_str(parse_url($url, PHP_URL_QUERY), $queryParameters);

// Extract fragment
$fragment = parse_url($url, PHP_URL_FRAGMENT);

echo "Domain: " . $domain . "<br>";
echo "Path: " . $path . "<br>";
echo "Query Parameters: ";
print_r($queryParameters);
echo "<br>";
echo "Fragment: " . $fragment . "<br>";