Youtube I.D parsing for new URL formats

前端 未结 5 1811
借酒劲吻你
借酒劲吻你 2020-12-02 12:18

This question has been asked before and I found this:

Reg exp for youtube link

but I\'m looking for something slightly different.

I need to match th

5条回答
  •  渐次进展
    2020-12-02 13:09

    Currently I'm using this:

    function _getYoutubeVideoId($url)
    {
      $parts = parse_url($url);
    
      //For seriously malformed urls
      if ($parts === false) {
         return false;
      }
    
      switch ($parts['host']) {
         case 'youtu.be':
            return substr($parts['path'], 1);
            break;
         case 'youtube.com':
         case 'www.youtube.com':
            parse_str($parts['query'], $params);
            return $params['v'];
            break;
         default:
            return false;
            break;
      } 
    }
    

    It could be extended, but right now it works for most of the cases

提交回复
热议问题