Parse YouTube video id with preg_match

I am trying to parse the youtube video url id using preg_match. I found a regex on this site that seems to work;

(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+ 

As shown in this picture:

alt text

My PHP looks like this, but it does not work (gives an unknown modifier '[' error) ...

 <? $subject = "http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1"; preg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches); print "<pre>"; print_r($matches); print "</pre>"; ?> 

Greetings

+61
php regex youtube parsing
May 29 '10 at 20:19
source share
12 answers

This regular expression captures the identifier from all the URLs that I could find ... There could be more, but I could not find links to them anywhere. If you come across one that doesn't match, please leave a comment with a URL and I will try to update the regex to match your URL.

 if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) { $video_id = $match[1]; } 

Here is an example of the URLs that match this regex: (there may be more content after the specified URL to be ignored)

It also works with the url youtube-nocookie.com with the same settings.

It will also pull the identifier from the URL into the embed code (both iframe and object tags)

+222
Jun 17 '11 at 6:44
source share

Better use parse_url and parse_str to parse the URL and query string:

 $subject = "http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1"; $url = parse_url($subject); parse_str($url['query'], $query); var_dump($query); 
+11
May 29 '10 at 20:21
source share

I had to deal with this for a PHP class that I wrote a few weeks ago and ended up with a regular expression that matches any type of string: with or without a URL scheme, with or without a subdomain, youtube.com URL string, youtu.be string URL and handling of all kinds of sorting parameters. You can check this on GitHub or just copy and paste the code block below:

 /** * Check if input string is a valid YouTube URL * and try to extract the YouTube Video ID from it. * @author Stephan Schmitz <eyecatchup@gmail.com> * @param $url string The string that shall be checked. * @return mixed Returns YouTube Video ID, or (boolean) false. */ function parse_yturl($url) { $pattern = '#^(?:https?://)?(?:www\.)?(?: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; } 

To explain the regex, here's a spilled version:

 /** * Check if input string is a valid YouTube URL * and try to extract the YouTube Video ID from it. * @author Stephan Schmitz <eyecatchup@gmail.com> * @param $url string The string that shall be checked. * @return mixed Returns YouTube Video ID, or (boolean) false. */ function parse_yturl($url) { $pattern = '#^(?:https?://)?'; # Optional URL scheme. Either http or https. $pattern .= '(?:www\.)?'; # Optional www subdomain. $pattern .= '(?:'; # Group host alternatives: $pattern .= 'youtu\.be/'; # Either youtu.be, $pattern .= '|youtube\.com'; # or youtube.com $pattern .= '(?:'; # Group path alternatives: $pattern .= '/embed/'; # Either /embed/, $pattern .= '|/v/'; # or /v/, $pattern .= '|/watch\?v='; # or /watch?v=, $pattern .= '|/watch\?.+&v='; # or /watch?other_param&v= $pattern .= ')'; # End path alternatives. $pattern .= ')'; # End host alternatives. $pattern .= '([\w-]{11})'; # 11 characters (Length of Youtube video ids). $pattern .= '(?:.+)?$#x'; # Optional other ending URL parameters. preg_match($pattern, $url, $matches); return (isset($matches[1])) ? $matches[1] : false; } 
+7
May 10 '12 at 4:40
source share

I improved the regular expression from the leader's answer. It also captures the identifier from all URLs, but more correctly .

 if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/|(?:v|e(?:mbed)?)/|[\w\-?&!#=,;]*[?&]v=)|youtu\.be/)([\w-]{11})(?:[^\w-]|\Z)%i', $url, $match)) { $video_id = $match[1]; } 

In addition, it correctly processes invalid identifiers that contain more than 11 characters.

http://www.youtube.com/watch?v=0zM3nApSvMgDw3qlxF

+5
Sep 28 '14 at 17:40
source share

Using

  preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $subject, $matches); 
+2
May 29 '10 at 20:22
source share

You forgot to escape the slash character. So this one should do the job:

 preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]\/)[^&\n]+|(?<=v=)[^&\n]+#", $subject, $matches); 
+1
Apr 14 2018-11-11T00:
source share

Parse Launch Option for BBcode ( https://developers.google.com/youtube/player_parameters#start )

example: [yt]http://www.youtube.com/watch?v=G059ou-7wmo#t=58[/yt]

PHP regex:

 '#\[yt\]https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/ytscreeningroom\?v=|/feeds/api/videos/|/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=#&+%\w-]*(t=(\d+))?\[/yt\]#Uim' 

replace:

 '<iframe id="ytplayer" type="text/html" width="639" height="360" src="http://www.youtube.com/embed/$1?rel=0&vq=hd1080&start=$3" frameborder="0" allowfullscreen></iframe>' 
+1
Nov 10 '14 at
source share

I have not seen anyone directly access the PHP error, so I will try to explain.

The reason for the [Unknown] [Unknown] error is because you forgot to wrap your regular expression in delimiters. PHP just takes the first character as a delimiter if it's not an alphanumeric, non-whitespace ASCII character. So in your regex:

 preg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches); 

PHP thinks what you mean ( like an opening delimiter. Then it finds what it thinks is your closing delimiter, the next one ) and assumes that it follows the template modifiers. However, it discovers that your first template modifier, the next character after the first ) , is [ . [ is obviously not a valid template modifier, so you get an error that you are executing.

The solution is to simply wrap your regular expression in delimiters and make sure that any delimiters in the regex that you want to match literally are reset. I like to use ~ as delimiters, b / c you rarely have to match the literal ~ in regex.

+1
Mar 07 '15 at 1:25
source share

use below code

 $url = "" // here is url of youtube video $pattern = getPatternFromUrl($url); //this will retun video id function getPatternFromUrl($url) { $url = $url.'&'; $pattern = '/v=(.+?)&+/'; preg_match($pattern, $url, $matches); //echo $matches[1]; die; return ($matches[1]); } 
0
May 29 '10 at 21:06
source share

it worked for me.

 $yout_url='http://www.youtube.com/watch?v=yxYjeNZvICk&blabla=blabla'; $videoid = preg_replace("#[&\?].+$#", "", preg_replace("#http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|)#i", "", $yout_url)); 
0
Nov 04 '13 at 21:38
source share

I found the https://github.com/kminek/url-id package on Github that does the parsing of an identifier with a URL - may be useful

0
May 17 '19 at 19:36
source share

Regex is perhaps the best approach, but simpler is to simply explode such a URL and limit the response.

 $youtube_url = 'http://www.youtube.com/watch?v=yxYjeNZvICk&blabla=blabla'; $youtube_id = substr(explode("v=", $youtube_url), 0, 7)); 
-one
Feb 02 '15 at 4:14
source share



All Articles