RegEx to get YouTube video ID from any YouTube URL

Take these URLs as an example:

This PHP function will NOT correctly get the identifier in case 1, but in case 2. Case 1 is very common, where NOTHING can appear behind the YouTube identifier.

/** * get YouTube video ID from URL * * @param string $url * @return string YouTube video id or FALSE if none found. */ function youtube_id_from_url($url) { $pattern = '%^# Match any YouTube URL (?:https?://)? # Optional scheme. Either http or https (?:www\.)? # Optional www subdomain (?: # Group host alternatives youtu\.be/ # Either youtu.be, | youtube\.com # or youtube.com (?: # Group path alternatives /embed/ # Either /embed/ | /v/ # or /v/ | /watch\?v= # or /watch\?v= ) # End path alternatives. ) # End host alternatives. ([\w-]{10,12}) # Allow 10-12 for 11 char YouTube id. $%x' ; $result = preg_match($pattern, $url, $matches); if (false !== $result) { return $matches[1]; } return false; } 

I think there should be a way that I can just search for "v =", no matter where it is in the url, and take the characters after that. Thus, no complicated RegEx is required. This is not true? Any ideas for beginners?

+8
php regex youtube youtube-api
source share
9 answers
 if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $id)) { $values = $id[1]; } else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $id)) { $values = $id[1]; } else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $id)) { $values = $id[1]; } else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $id)) { $values = $id[1]; } else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $id)) { $values = $id[1]; } else { // not an youtube video } 

This is what I use to extract the id from the youtube url. I think this works in all cases.

Please note that at the end of $ values ​​= id of the video

+27
source share

Instead of regex. I highly recommend parse_url() and parse_str() :

 $url = "http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player"; parse_str(parse_url( $url, PHP_URL_QUERY ), $vars ); echo $vars['v']; 

Done

+9
source share

You can just use parse_url and parse_str :

 $query_string = parse_url($url, PHP_URL_QUERY); parse_str($query_string); echo $v; 
+2
source share

Another simple way is parse_str() :

 <?php $url = 'http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player'; parse_str($url, $yt); // The associative array $yt now contains all of the key-value pairs from the querystring (along with the base 'watch' URL, but doesn't seem you need that) echo $yt['v']; // echos '8GqqjVXhfMU'; ?> 
0
source share

The parse_url suggestions are good. If you really want a regex, you can use this:

 /(?<=v=)[^&]+/` 
0
source share

I used the following templates because YouTube also has the youtube-nocookie.com domain:

 '@youtube(?:-nocookie)?\.com/watch[#\?].*?v=([^"\& ]+)@i', '@youtube(?:-nocookie)?\.com/embed/([^"\&\? ]+)@i', '@youtube(?:-nocookie)?\.com/v/([^"\&\? ]+)@i', '@youtube(?:-nocookie)?\.com/\?v=([^"\& ]+)@i', '@youtu\.be/([^"\&\? ]+)@i', '@gdata\.youtube\.com/feeds/api/videos/([^"\&\? ]+)@i', 

In your case, this would only mean expanding existing expressions with optional (-nocookie) for the regular YouTube.com URL as follows:

 if (preg_match('/youtube(?:-nocookie)\.com\/watch\?v=([^\&\?\/]+)/', $url, $id)) { 

If you modify the proposed expression so that it does NOT contain the final $, it should work as you intended. I also added -nocookie.

 /** * get YouTube video ID from URL * * @param string $url * @return string YouTube video id or FALSE if none found. */ function youtube_id_from_url($url) { $pattern = '%^# Match any YouTube URL (?:https?://)? # Optional scheme. Either http or https (?:www\.)? # Optional www subdomain (?: # Group host alternatives youtu\.be/ # Either youtu.be, |youtube(?:-nocookie)?\.com # or youtube.com and youtube-nocookie (?: # Group path alternatives /embed/ # Either /embed/ | /v/ # or /v/ | /watch\?v= # or /watch\?v= ) # End path alternatives. ) # End host alternatives. ([\w-]{10,12}) # Allow 10-12 for 11 char YouTube id. %x' ; $result = preg_match($pattern, $url, $matches); if (false !== $result) { return $matches[1]; } return false; } 
0
source share

SOLUTION for any YOUTUBE LINK:

 http://youtube.com/v/dQw4w9WgXcQ http://youtube.com/watch?v=dQw4w9WgXcQ http://www.youtube.com/watch?feature=player&v=dQw4w9WgXcQ&var2=bla http://youtu.be/dQw4w9WgXcQ 

==

stack overflow

0
source share

Here is one solution

 /** * credits goes to: http://stackoverflow.com/questions/11438544/php-regex-for-youtube-video-id * update: mobile link detection */ public function parseYouTubeUrl($url) { $pattern = '#^(?:https?://)?(?:www\.)?(?:m\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x'; preg_match($pattern, $url, $matches); return (isset($matches[1])) ? $matches[1] : false; } 

It can also work with mobile links.

0
source share

Here is my function to get Youtube ID!

 function getYouTubeId($url) { if (!(strpos($url, 'v=') !== false)) return false; $parse = explode('v=', $url); $code = $parse[1]; if (strlen($code) < 11) return false; $code = substr($code, 0, 11); return $code; } 
-one
source share

All Articles