How do I find all YouTube video ids in a string using a regex?
Asked Answered
C

10

95

I have a textfield where users can write anything.

For example:

Lorem Ipsum is simply dummy text. http://www.youtube.com/watch?v=DUQi_R4SgWo of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. http://www.youtube.com/watch?v=A_6gNZCkajU&feature=relmfu It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Now I would like to parse it and find all YouTube video URLs and their ids.

Any idea how that works?

Chaworth answered 29/4, 2011 at 9:32 Comment(1)
possible duplicate of Javascript REGEX: How to get youtube video id from URL?Quathlamba
V
298

A YouTube video URL may be encountered in a variety of formats:

  • latest short format: http://youtu.be/NLqAF9hrVbY
  • iframe: http://www.youtube.com/embed/NLqAF9hrVbY
  • iframe (secure): https://www.youtube.com/embed/NLqAF9hrVbY
  • object param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • object embed: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • watch: http://www.youtube.com/watch?v=NLqAF9hrVbY
  • users: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
  • ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
  • any/thing/goes!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
  • any/subdomain/too: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
  • more params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
  • query may have dot: http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
  • nocookie domain: http://www.youtube-nocookie.com

Here is a PHP function with a commented regex that matches each of these URL forms and converts them to links (if they are not links already):

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs($text) {
    $text = preg_replace('~(?#!js YouTubeId Rev:20160125_1800)
        # Match non-linked youtube URL in the wild. (Rev:20130823)
        https?://          # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)?  # Optional subdomain.
        (?:                # Group host alternatives.
          youtu\.be/       # Either youtu.be,
        | youtube          # or youtube.com or
          (?:-nocookie)?   # youtube-nocookie.com
          \.com            # followed by
          \S*?             # Allow anything up to VIDEO_ID,
          [^\w\s-]         # but char before ID is non-ID char.
        )                  # End host alternatives.
        ([\w-]{11})        # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w-]|$)       # Assert next char is non-ID or EOS.
        (?!                # Assert URL is not pre-linked.
          [?=&+%\w.-]*     # Allow URL (query) remainder.
          (?:              # Group pre-linked alternatives.
            [\'"][^<>]*>   # Either inside a start tag,
          | </a>           # or inside <a> element text contents.
          )                # End recognized pre-linked alts.
        )                  # End negative lookahead assertion.
        [?=&+%\w.-]*       # Consume any URL (query) remainder.
        ~ix', '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
        $text);
    return $text;
}

; // End $YouTubeId.

And here is a JavaScript version with the exact same regex (with comments removed):

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs(text) {
    var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
    return text.replace(re,
        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>');
}

Notes:

  • The VIDEO_ID portion of the URL is captured in the one and only capture group: $1.
  • If you know that your text does not contain any pre-linked URLs, you can safely remove the negative lookahead assertion which tests for this condition (The assertion beginning with the comment: "Assert URL is not pre-linked.") This will speed up the regex somewhat.
  • The replace string can be modified to suit. The one provided above simply creates a link to the generic "http://www.youtube.com/watch?v=VIDEO_ID" style URL and sets the link text to: "YouTube link: VIDEO_ID".

Edit 2011-07-05: Added - hyphen to ID char class

Edit 2011-07-17: Fixed regex to consume any remaining part (e.g. query) of URL following YouTube ID. Added 'i' ignore-case modifier. Renamed function to camelCase. Improved pre-linked lookahead test.

Edit 2011-07-27: Added new "user" and "ytscreeningroom" formats of YouTube URLs.

Edit 2011-08-02: Simplified/generalized to handle new "any/thing/goes" YouTube URLs.

Edit 2011-08-25: Several modifications:

  • Added a Javascript version of: linkifyYouTubeURLs() function.
  • Previous version had the scheme (HTTP protocol) part optional and thus would match invalid URLs. Made the scheme part required.
  • Previous version used the \b word boundary anchor around the VIDEO_ID. However, this will not work if the VIDEO_ID begins or ends with a - dash. Fixed so that it handles this condition.
  • Changed the VIDEO_ID expression so that it must be exactly 11 characters long.
  • The previous version failed to exclude pre-linked URLs if they had a query string following the VIDEO_ID. Improved the negative lookahead assertion to fix this.
  • Added + and % to character class matching query string.
  • Changed PHP version regex delimiter from: % to a: ~.
  • Added a "Notes" section with some handy notes.

Edit 2011-10-12: YouTube URL host part may now have any subdomain (not just www.).

Edit 2012-05-01: The consume URL section may now allow for '-'.

Edit 2013-08-23: Added additional format provided by @Mei. (The query part may have a . dot.

Edit 2013-11-30: Added additional format provided by @CRONUS: youtube-nocookie.com.

Edit 2016-01-25: Fixed regex to handle error case provided by CRONUS.

Valentia answered 29/4, 2011 at 9:32 Comment(49)
Thanks for this! However I found out that hyphens are also allowed in the id so (\w{10-12}) should be ([\w-]{10,12})Iconoclasm
@littleFluffyKitty: I knew that the underscore was ok but the dash is news to me. Is there a spec somewhere which defines these valid-ID characters?Valentia
I haven't seen a spec, though I did search one out. I just noticed the dash in some links in the wilds of the internet. For example: youtube.com/watch?v=CLPk-6_xgiYIconoclasm
@littleFluffyKitty: Thanks for the heads up. Have updated the answer to include the hyphen as a valid ID char.Valentia
@Valentia I've come across other URL's like such: youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4 and I can't seem to modify your RegEx to catch this one Thanx in advance!Bilharziasis
@Stanley: Thanks for the new URL format. The regex is updated to handle this one (and should now work for just about any other one as well). Cheers!Valentia
@Valentia - I just discovered a URL in simple browsing that doesn't seem to work: youtube.com/watch?v=VERGbYDuJ-4&feature=feedu. Can this one be fixed? Thank you so much - this is quite a project to write the definitive YouTube ID regex!Belmonte
@Belmonte - The linkifyYouTubeURLs() function does a simple match-and-replace operation which requires that the matched URL be a valid URL so that it can be directly inserted into the href attribute of an A tag. Thus, the regex above does require the URL scheme (either http or https). If you are willing to do a bit more logic in the function, then the regex could be modified to match the example you cite (which has no scheme). To do this, simply wrap the scheme expression at the beginning and add a ? like so: (?:https?://)?. Thanks for the comment!Valentia
@Valentia - I finally identified the issue that's been cropping up for me - your Javascript regex above works with everything except the ?v= format in watch and ytscreeningroom. I was concerned because copying a regular video's link from the address bar (which most of my users are doing) is triggering this problem. Thank you for your comment yesterday regarding http/s!Belmonte
@Valentia nice work! Now, how many country specific youtube domains are there and is it reasonable to bake them into this regex?Liberty
I think you've missed the possibility of _ being in the id.Tinnitus
@Kevin B: No, the \w is equivalent to [A-Za-z0-9_] which includes the underscore.Valentia
I can't get this to match youtube.com/embed/… Because it will catch transparent and the id.Misdoubt
@Doug Tabuchi - this regex requires that the: http:// or https:// URL scheme to be present.Valentia
That was a paste fail. This url matches transparent instead of vDGrfhJH1P4. youtube.com/embed/…Misdoubt
Also looks like when you paste a link in SO it linkify's it so http:// was present in the paste but is removed by SO.Misdoubt
@hakre - So anyone can come in and edit the content of this answer without my notification or approval? I thought this was supposed to be my answer??? If this is the case then I am going to rethink my contributions to this site...Valentia
@ridgerunner: I didn't wanted to insult you with my edit (done in the best intentions), but this might be shocking for you now: The FAQ - however normally this turns out to work pretty well.Tarkany
@Tarkany - Wow that actually is shocking - thanks for pointing that out. I'm all for open source, and I certainly have no problem with others forking my (very carefully and deliberately crafted) answers (which may look like like they need "fixing" to someone for whatever reason). Now that I know I have zero control over the quality of the sources to my own answers, I will certainly not be contributing further, Shit, and I really liked it here. D'oh! Thanks again for the head's up.Valentia
@ridgerunner: If you're not confident with an edit, you can roll-back. Additionally like on wikipedia the whole history is kept with your credits. I've seen you really gardened the answer over time, so it wouldbe a pitty to loose you here.Tarkany
@Valentia Your regex knowledge surpasses all levels. Nice job. ONe question I can seem to modifie to accept this URL: http://www.youtube.com/watch?feature=v-feature&v=317a815FLWQDesultory
Hello Ridgerunner! Your regular expression has worked like a charm. But, i have one small issue regarding this. I am getting the video id from youtube url in java. I am using the following code pastebin.com/ZrPcHZBX which is working fine for all the url formats which u have mentioned above except for the second one. Please help me i dont know where i am doing mistake ...Fulvous
Here is one that doesn't worked: youtube.com/watch?v=E1IPnnttL9k&feature=youtu.beGlossectomy
This is absolutely amazing! Youtube URL's are extremely hard to decipher but this catches 99% of them. I'm trying to generate thumbnails from the ID from twitter and this works pretty well!Marine
I've used this code for years, it's brilliant but I've just posted a new question regarding 'start' times of a youtube video here: #15148493Exhibition
This works great, but fails with this (new?) querystring param: feature=youtu.be. Changing [?=&+%\w-]* to [?=&+%\w-\.]* on your "consume remaining url" line does the trick. Thanks!Kilogram
@Mei - Thanks for the heads up about the dot-in-query style. Have updated the answer. p.s. Looks like I'm back (couldn't stay away long!)Valentia
youtube-nocookie.com should be addedStepdaughter
@ridgerunner: It's not working for <iframe width="560" height="315" src="//www.youtube.com/embed/gL1dEBZ6Vyc" frameborder="0" allowfullscreen></iframe>Regina
@gowri - Its not supposed to. The given pattern finds YT URL's that are not already linked. Read the second note for tips on how to modify the pattern to remove this requirement.Valentia
@CRONUS - Thanks for the nocookie heads up. Just now added this to the pattern.Valentia
@Valentia this is very helpful - thank you. However I just came across an URL which causes the regex to incorrectly catch desktop_uri as the video ID: http://youtube.com/watch?v=cpiU14HTypk&desktop_uri=%2Fwatch%3Fv%3DcpiU14HTypkSemantics
Quick and dirty hack to prevent desktop_uri matches, replacing the corresponding line: ((?!desktop_uri)[\w-]{11}) # $1: VIDEO_ID is exactly 11 chars.Semantics
this kind of link is not handled : youtube.com/watch?feature=player_detailpage&v=_G2ZHa0Whg0Jola
@Let Aurn - I just tested it and the regex matches your example YT link just fine (and correctly extracts the YT ID as well).Valentia
Just as an note for people trying to use this regex outside of PHP; in certain other languages (including C#) the \w used in regex does not exactly evaluate to [A-Za-z0-9_]; it can include accented characters and alphabets from the current locale. PHP, however, was designed pretty much only with the US locale in mind, so it's a matter of taste whether or not to use it here. If you're worried about false positives with accented characters, make sure you use the expanded notation, and not \w.Bagnio
doesn't work when t is the first paramater: youtube.com/watch?t=48&v=Opcu8ZJYE6Sax
@Sax - You are correct - this solution does not match your example (youtube.com/watch?t=48&v=Opcu8ZJYE6) because it is missing the: http://. This solution requires the http:// scheme to form a absolute URI.Valentia
Looks like this gives the following error: FATAL ERROR syntax error, unexpected '"][^<>]*> # Either inside a s' (T_CONSTANT_ENCAPSED_STRING)Sikora
Got a link with additional query, that is not matched correctly. http://www.youtube.com/watch?v=4L1cvtAx4vc&utm_campaign=one%20abcdefghi%20two. I get 20abcdefghi instead of 4L1cvtAx4vc. Any solution to get it right without decoding url or other similar stuff?Stepdaughter
@CRONUS - Fixed it - see above.. Thanks for the erroneous test case! 8^)Valentia
not working with consecutive links, i.e. http://youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_UShttp://youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_USMaltese
@Maltese - No, it won't work with two URL stuck together like your example.Valentia
I searched all answers on how to find youtube video id and this is the best. I also tested it on all examples possible and it works. The only thing that doesn't work is when url doesn't start with http(s), but that is easily fixable, you just need to put (?:https?:)?(?:\/\/)? instead https?:// , also here is jsfiddle url with all examples i have tested.Summation
Need exception for "videoseries". https://www.youtube.com/embed/videoseries?list= ThanksHandmaid
This does not work if URL has the time_continue parameter. Example: https://www.youtube.com/watch?time_continue=4&v=IDSTRINGSikora
@Henrik Petterson - Your example IDSTRING ID string is not exactly 11 characters wide (all IDs must be 11 chars). My testing shows that the regex matches just fine when the ID is 11 (valid=/[\w-]{11}/) chars long even when preceded by time_continue=4&. Not sure why it is failing for you.Valentia
@Summation - Making the https?//: optional is asking for trouble. By doing this, the regex could very well match text that is not a youtube link (and will aslo slow it down). The logic of this regex depends upon requiring the specificity of the URL scheme prefix found in absolute URIsValentia
@Maxim Pokrovskii - Yes you are correct. A false ID match occurs with URIs like: https://www.youtube.com/embed/videoseries?v=youtubeid01 - (the iD is incorrectly matched as videoseries). The problem occurs if there is any 'word' having exactly 11 chars preceding the ID. Unfortunately this behavior is not one that is easy to fix considering the simplistic logic used in this regex.Valentia
Q
10

Here's a method I once wrote for a project that extracts YouTube and Vimeo video keys:

/**
 *  strip important information out of any video link
 *
 *  @param  string  link to a video on the hosters page
 *  @return mixed  FALSE on failure, array on success
 */
function getHostInfo ($vid_link)
{
  // YouTube get video id
  if (strpos($vid_link, 'youtu'))
  {
    // Regular links
    if (preg_match('/(?<=v\=)([\w\d-_]+)/', $vid_link, $matches))
      return array('host_name' => 'youtube', 'original_key' => $matches[0]); 
    // Ajax hash tag links
    else if (preg_match('§([\d\w-_]+)$§i', $vid_link, $matches))
      return array('host_name' => 'youtube', 'original_key' => $matches[0]);
    else
      return FALSE;
  }
  // Vimeo get video id
  elseif (strpos($vid_link, 'vimeo'))
  {
    if (preg_match('§(?<=/)([\d]+)§', $vid_link, $matches))
      return array('host_name' => 'vimeo', 'original_key' => $matches[0]); 
    else
      return FALSE;
  }
  else
    return FALSE;
}
  1. Find a regex that will extract all links from a text. Google will help you there.
  2. Loop all the links and call getHostInfo() for each
Quack answered 29/4, 2011 at 9:38 Comment(3)
great thanks! slight mod if(strpos($vid_link, 'youtu')) will capture the short url youtu.be in addition to the common url's.Cleocleobulus
you're welcome. thanks for the update, I edited in the change. on a side note, ridgerunner's regex seems to be the real deal and I recommend using it over my simple thing. cheersQuack
exactly what i was looking for. spot on mate! +1Limoges
E
7

While ridgerunner's answer is the basis for my answer, his does NOT solve for all urls and I don't believe it is capable of it, due to multiple possible matches of VIDEO_ID in a YouTube URL. My regex includes his aggressive approach as a last resort, but attempts all common matchings first, vastly reducing the possibility of a wrong match later in the URL.

This regex:

/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-]*/ig;

Handles all of the cases originally referenced in ridgerunners examples, plus any url that might happen to have an 11 character sequence later in the url. ie:

http://www.youtube.com/watch?v=GUEZCxBcM78&feature=pyv&feature=pyv&ad=10059374899&kw=%2Bwingsuit

Here is a working sample that tests all of the sample YouTube urls:

http://jsfiddle.net/DJSwc/5/

Enduring answered 1/5, 2012 at 23:37 Comment(0)
B
2

Use:

<?php

    // The YouTube URL string

    $youtube_url='http://www.youtube.com/watch?v=8VtUYvwktFQ';

    // Use regex to get the video ID

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

    preg_match($regex, $youtube_url, $id);

    // Plug that into our HTML
?>
Bronchi answered 29/4, 2011 at 9:39 Comment(0)
C
2

Okay, I made a function of my own. But I believe it's pretty inefficient. Any improvements are welcome:

function get_youtube_videos($string) {

    $ids = array();

    // Find all URLs
    preg_match_all('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $string, $links);

    foreach ($links[0] as $link) {
        if (preg_match('~youtube\.com~', $link)) {
            if (preg_match('/[^=]+=([^?]+)/', $link, $id)) {
                $ids[] = $id[1];
            }
        }
    }
    return $ids;
}
Chaworth answered 29/4, 2011 at 10:2 Comment(1)
If you are looking only for links from youtube.com why do you want to build at first a list with all links? And I think its not necessary to use 3 different regexes.Sisterhood
S
2

Try

[^\s]*youtube\.com[^\s]*?v=([-\w]+)[^\s]*

You will find the video IDs' in the first capturing group. What I don't know is what is a valid Video ID? At the moment I check for v= and capture all -A-Za-z0-9_.

I checked it online here on rubular with your sample string.

Sisterhood answered 29/4, 2011 at 10:6 Comment(0)
M
2

The original poster asked "I would like to parse it and find all YouTube video URLs and their ids." I switched the most popular answer above to a preg_match and returned the video id and URL.

Get YouTube URL and ID from post:

$match[0] = Full URL
$match[1] = video ID

function get_youtube_id($input) {
    $input = preg_match('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*>|</a>))[?=&+%\w.-]*~ix',
                        $input, $match);
    return $match;
}
Mendes answered 24/5, 2015 at 19:33 Comment(0)
H
1

I tried a simple expression to get only the videoid:

[?&]v=([^&#]*)

Check it working online here at phpliveregex.

Heron answered 12/11, 2014 at 7:40 Comment(0)
G
0

Find a YouTube link easily from a string:

function my_url_search($se_action_data)
{
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $se_action_data, $matches);
    $get_url=array_reverse($matches[0]);
    return array_unique($get_url);
}
echo my_url_search($se_action_data)
Gladiate answered 19/6, 2014 at 13:10 Comment(1)
This is not just for YoutTube, it will match other urls from content.Maleki
V
0
String urlid="" ;
String  url="http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s";
Pattern pattern =Pattern.compile("(?:http|https|)(?::\\/\\/|)(?:www.|)(?:youtu\\.be\\/|youtube\\.com(?:\\/embed\\/|\\/v\\/|\\/watch\\?v=|\\/ytscreeningroom\\?v=|\\/feeds\\/api\\/videos\\/|\\/user\\\\S*[^\\w\\-\\s]|\\S*[^\\w\\-\\s]))([\\w\\-\\_]{11})[a-z0-9;:@#?&%=+\\/\\$_.-]*");
Matcher result = pattern.matcher(url);
    if (result.find())
    {
         urlid=result.group(1);

    }

This code in java works absolutely fine for all youtube urls at present.

Vizzone answered 26/2, 2018 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.