How can the parse_url function in PHP be utilized to efficiently handle URL parsing and filtering tasks?

The parse_url function in PHP can be utilized to efficiently handle URL parsing and filtering tasks by extracting various components of a URL such as the scheme, host, path, query, and fragment. This function can be used to validate URLs, extract specific parts for further processing, or sanitize user input containing URLs.

$url = "https://www.example.com/page?param=value#section";
$parsed_url = parse_url($url);

if($parsed_url){
    echo "Scheme: " . $parsed_url['scheme'] . "\n";
    echo "Host: " . $parsed_url['host'] . "\n";
    echo "Path: " . $parsed_url['path'] . "\n";
    echo "Query: " . $parsed_url['query'] . "\n";
    echo "Fragment: " . $parsed_url['fragment'] . "\n";
} else {
    echo "Invalid URL";
}