RegEx pattern to get the YouTube video ID from any YouTube URL

后端 未结 9 1873
南方客
南方客 2020-11-27 17:48

Let\'s take these URLs as an example:

  1. http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player
  2. http://www.youtube.com/watch?v=8Gqqj
相关标签:
9条回答
  • 2020-11-27 18:10
    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 an youtube url. I think it works in all cases.

    Note that at the end $values = id of the video

    0 讨论(0)
  • 2020-11-27 18:12

    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
    

    ==

    https://stackoverflow.com/a/20614061/2165415

    0 讨论(0)
  • 2020-11-27 18:17

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

    /(?<=v=)[^&]+/`
    
    0 讨论(0)
  • 2020-11-27 18:20

    You could just use parse_url and parse_str:

    $query_string = parse_url($url, PHP_URL_QUERY);
    parse_str($query_string);
    echo $v;
    
    0 讨论(0)
  • 2020-11-27 18:20

    Another easy way is using 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 讨论(0)
  • 2020-11-27 18:23

    I have used the following patterns because YouTube has a youtube-nocookie.com domain too:

    '@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 it would only mean to extend the existing expressions with an optional (-nocookie) for the regular YouTube.com URL like so:

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

    If you change your proposed expression to NOT contain the final $, it should work like you intended. I added the -nocookie as well.

    /**
     * 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 讨论(0)
提交回复
热议问题