How to Get Youtube Or Short Reels Video Id from URL in PHP?

Get Youtube Video Id from Youtube Video URL in PHP, PHP Regex to get youtube video ID, Extract the YouTube Video ID from a URL Using PHP

Hi friends Today, I will tell you through this tutorial that you can get the id of video from youtube video url via php script code. I will tell you to get the id of youtube video in 4 ways. So let’s try to understand this by example.

Get the video ID from a YouTube URL using PHP, you can use a combination of string manipulation functions or regular expressions. YouTube video IDs are typically found in the `v` parameter of the URL. Here’s how you can extract it:

Example YouTube URLs:

1. `https://www.youtube.com/watch?v=dQw4w9WgXcQ`
2. `https://youtu.be/dQw4w9WgXcQ`
3. `https://www.youtube.com/embed/dQw4w9WgXcQ`

The video ID in these examples is `dQw4w9WgXcQ`.

Method 1: Using `parse_url()` and `parse_str()`

This method works for standard YouTube URLs (e.g., `https://www.youtube.com/watch?v=VIDEO_ID`).

<?php
function getYouTubeVideoId($url) {

// Parse the URL to get the query string
$queryString = parse_url($url, PHP_URL_QUERY);

// Parse the query string into an associative array
parse_str($queryString, $params);

// Return the 'v' parameter (video ID)
return $params['v'] ?? null;
}

// Example usage
$url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
$videoId = getYouTubeVideoId($url);

echo "Video ID: " . $videoId; // Output: Video ID: dQw4w9WgXcQ
?>

Method 2: Using Regular Expressions

This method works for all types of YouTube URLs, including short URLs (`youtu.be`) and embedded URLs.

<?php
function getYouTubeVideoId($url) {

// Regular expression to match YouTube video IDs
$pattern = '/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/';

// Perform the match
if (preg_match($pattern, $url, $matches)) {
return $matches[1]; // The video ID is in the first capturing group
}
return null; // Return null if no match is found
}

// Example usage
$url = "https://youtu.be/dQw4w9WgXcQ";
$videoId = getYouTubeVideoId($url);

echo "Video ID: " . $videoId; // Output: Video ID: dQw4w9WgXcQ
?>

Explanation of the Regular Expression:

-> `(?:…)`: Non-capturing group.
-> `youtube\.com\/`: Matches `youtube.com/`.
-> `(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)`: Matches various YouTube URL formats:
-> `[^\/\n\s]+\/\S+\/`: Matches paths like `user/…` or `channel/…`.
-> `(?:v|e(?:mbed)?)\/`: Matches `/v/` or `/embed/`.
-> `\S*?[?&]v=`: Matches the `v=` parameter in the query string.
-> `youtu\.be\/`: Matches short URLs like `youtu.be/VIDEO_ID`.
-> `([a-zA-Z0-9_-]{11})`: Captures the 11-character video ID.

Method 3: Handling Edge Cases

If you want to handle edge cases (e.g., URLs with additional parameters or fragments), you can refine the regular expression or add additional checks.

<?php
function getYouTubeVideoId($url) {
$pattern = '/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/';
if (preg_match($pattern, $url, $matches)) {
return $matches[1];
}
return null;
}

// Example usage
$urls = [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"https://youtu.be/dQw4w9WgXcQ",
"https://www.youtube.com/embed/dQw4w9WgXcQ",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be"
];

foreach ($urls as $url) {
$videoId = getYouTubeVideoId($url);
echo "URL: $url\nVideo ID: $videoId\n\n";
}
?>

Output:

URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ
Video ID: dQw4w9WgXcQ

URL: https://youtu.be/dQw4w9WgXcQ
Video ID: dQw4w9WgXcQ

URL: https://www.youtube.com/embed/dQw4w9WgXcQ
Video ID: dQw4w9WgXcQ

URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be
Video ID: dQw4w9WgXcQ

Method 4:-

<?php 
$link = "http://www.youtube.com/watch?v=jiXdSJYRHA0";
$video_id = explode("?v=", $link);
echo $video_id = $video_id[1];
?>

Notes:
-> Always validate the input URL to ensure it is a valid YouTube URL.
-> The video ID is always 11 characters long and consists of alphanumeric characters, hyphens (`-`), and underscores (`_`).