首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Youtube I.D解析新的URL格式

Youtube I.D解析新的URL格式
EN

Stack Overflow用户
提问于 2011-10-08 05:52:35
回答 4查看 18.5K关注 0票数 21

这个问题以前也被问过,我发现是这样的:

Reg exp for youtube link

但我在寻找一些稍微不同的东西。

我需要匹配Youtube身份证本身与所有可能的youtube链接格式兼容。并不是完全以youtube.com开头。

例如:

http://www.youtube.com/watch?v=-wtIMTCHWuI

http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1

http://youtu.be/-wtIMTCHWuI

http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json

http://s.ytimg.com/yt/favicon-wtIMTCHWuI.ico

http://i2.ytimg.com/vi/-wtIMTCHWuI/hqdefault.jpg

有没有一个聪明的策略,我可以用来匹配与所有这些格式兼容的视频I.D -wtIMTCHWuI。我在考虑字符计数和匹配= ? / . &字符。

EN

回答 4

Stack Overflow用户

发布于 2012-05-10 05:41:21

我不得不为我几周前编写的一个PHP类处理这个问题,最后得到了一个可以匹配任何类型字符串的正则表达式:有或没有URL方案、有或没有子域、youtube.com URL字符串、youtu.be URL字符串以及处理所有类型的参数排序。你可以把它签出at GitHub,或者简单地复制并粘贴下面的代码块:

/**
 *  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\.|m\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v/|watch\?v=|watch\?.+&v=))([\w-]{11})(?![\w-])#';
    preg_match($pattern, $url, $matches);
    return (isset($matches[1])) ? $matches[1] : false;
}

测试用例:https://3v4l.org/GEDT0

JavaScript版本:https://stackoverflow.com/a/10315969/624466

为了解释正则表达式,这里有一个拆分版本:

/**
 *  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, or protocol-relative.
             . '(?:www\.|m\.)?'      #  Optional www or m subdomain.
             . '(?:'                 #  Group host alternatives:
             .   'youtu\.be/'        #    Either youtu.be,
             .   '|youtube\.com/'    #    or youtube.com
             .     '(?:'             #    Group path alternatives:
             .       'embed/'        #      Either /embed/,
             .       '|v/'           #      or /v/,
             .       '|watch\?v='    #      or /watch?v=,
             .       '|watch\?.+&v=' #      or /watch?other_param&v=
             .     ')'               #    End path alternatives.
             . ')'                   #  End host alternatives.
             . '([\w-]{11})'         # 11 characters (Length of Youtube video ids).
             . '(?![\w-])#';         # Rejects if overlong id.
    preg_match($pattern, $url, $matches);
    return (isset($matches[1])) ? $matches[1] : false;
}
票数 50
EN

Stack Overflow用户

发布于 2011-11-04 05:24:21

我找到了这个代码this link

<?php 
/** 
 *  parse_youtube_url() PHP function 
 *  Author: takien 
 *  URL: http://takien.com 
 *  
 *  @param  string  $url    URL to be parsed, eg:  
 *                            http://youtu.be/zc0s358b3Ys,  
 *                            http://www.youtube.com/embed/zc0s358b3Ys
 *                            http://www.youtube.com/watch?v=zc0s358b3Ys 
 *  @param  string  $return what to return 
 *                            - embed, return embed code 
 *                            - thumb, return URL to thumbnail image
 *                            - hqthumb, return URL to high quality thumbnail image.
 *  @param  string     $width  width of embeded video, default 560
 *  @param  string  $height height of embeded video, default 349
 *  @param  string  $rel    whether embeded video to show related video after play or not.

 */  

 function parse_youtube_url($url,$return='embed',$width='',$height='',$rel=0){ 
    $urls = parse_url($url); 

    //expect url is http://youtu.be/abcd, where abcd is video iD
    if($urls['host'] == 'youtu.be'){  
        $id = ltrim($urls['path'],'/'); 
    } 
    //expect  url is http://www.youtube.com/embed/abcd 
    else if(strpos($urls['path'],'embed') == 1){  
        $id = end(explode('/',$urls['path'])); 
    } 
     //expect url is abcd only 
    else if(strpos($url,'/')===false){ 
        $id = $url; 
    } 
    //expect url is http://www.youtube.com/watch?v=abcd 
    else{ 
        parse_str($urls['query']); 
        $id = $v; 
    } 
    //return embed iframe 
    if($return == 'embed'){ 
        return '<iframe width="'.($width?$width:560).'" height="'.($height?$height:349).'" src="http://www.youtube.com/embed/'.$id.'?rel='.$rel.'" frameborder="0" allowfullscreen>'; 
    } 
    //return normal thumb 
    else if($return == 'thumb'){ 
        return 'http://i1.ytimg.com/vi/'.$id.'/default.jpg'; 
    } 
    //return hqthumb 
    else if($return == 'hqthumb'){ 
        return 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg'; 
    } 
    // else return id 
    else{ 
        return $id; 
    } 
} 
?>

我也在处理这件事,所以如果你找到更好的解决方案,请让我知道。它不能做你需要的开箱即用的图像,但它可以很容易地进行调整。

票数 3
EN

Stack Overflow用户

发布于 2012-04-01 13:36:10

目前我使用的是:

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;
  } 
}

它可以扩展,但目前它适用于大多数情况

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7693218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档