How can PHP functions like parse_url and parse_str be utilized to extract specific parts of a URL, such as a YouTube video ID?

To extract a YouTube video ID from a URL, you can use PHP functions like parse_url to parse the URL and parse_str to extract the query string parameters. By breaking down the URL into its components and then extracting the video ID parameter, you can easily retrieve the specific part you need.

$url = "https://www.youtube.com/watch?v=VIDEO_ID_HERE";
$video_id = '';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
if(isset($params['v'])){
    $video_id = $params['v'];
}
echo $video_id;