How do I get the YouTube video ID from a URL?

前端 未结 30 2410
北恋
北恋 2020-11-22 03:06

I want to get the v=id from YouTube’s URL with JavaScript (no jQuery, pure JavaScript).

Example YouTube URL formats

http://www.youtube.c

30条回答
  •  一生所求
    2020-11-22 03:33

    Try this one -

    function getYouTubeIdFromURL($url) 
    {
      $pattern = '/(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})/i';
      preg_match($pattern, $url, $matches);
    
      return isset($matches[1]) ? $matches[1] : false;
    }
    

提交回复
热议问题