Youtube I.D解析新的URL格式 [英] Youtube I.D parsing for new URL formats

查看:169
本文介绍了Youtube I.D解析新的URL格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之前已经问过了,我发现这个:

Reg exp for youtube link



但是我正在寻找稍微不同的东西。

我需要匹配Youtube I.D本身兼容所有可能的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



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

解决方案

我必须处理这个问题,我为几个星期前写了一个PHP类,结束了与任何类型的字符串匹配的正则表达式:有或没有URL方案,有或没有子域,youtube.com URL字符串,youtu.be URL字符串和处理所有类型的参数排序。您可以在GitHub上 或者直接复制粘贴下面的代码块:

  / ** 
*检查输入字符串是否有效YouTube网址
*并尝试从中提取YouTube视频ID。
* @author Stephan Schmitz< eyecatchup@gmail.com>
* @param $ url字符串应该被检查的字符串。
* @return mixed返回YouTube视频ID,或(boolean)false。
* /
function parse_yturl($ url)
{
$ pattern ='#^(?:https::// | //)?(?:www \ 。?| m\)(?: youtu\.be / | youtube\.com /(?:嵌入/ | v / | watch\ v = | watch\ +&安培;?v = ))([\w - ] {11})([\w - ])#'?!;
preg_match($ pattern,$ url,$ matches);
return(isset($ matches [1]))? $ matches [1]:false;

测试案例: https://3v4l.org/GEDT

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



解释正则表达式,这是一个分裂的版本:

  / ** 
*检查输入的字符串是否为有效的YouTube网址
*,并尝试从中提取YouTube视频ID它。
* @author Stephan Schmitz< eyecatchup@gmail.com>
* @param $ url字符串应该被检查的字符串。
* @return mixed返回YouTube视频ID,或(boolean)false。
* /
function parse_yturl($ url)
{
$ pattern ='#^(?:https::// | //)?'#可选的URL方案。可以是http或https,也可以是协议相关的。
。 '(?:www \。| m \。)?'#可选www或m子域名。
。 '(#:##主机替代品:
。'youtu\.be /'#youtu.be,
。'| youtube\.com /'#or youtube.com
。'(?:'#Group path alternatives:
。'embed /'#要么/ embed /,
。'| v /'#或/ v /,
'。 | watch \?v ='#or / watch?v =,
。'| watch \。。+& v ='#或/ watch?other_param& v =
') '#结束路径选择。
。')'#结束主机替代。
。 '([\w - ] {11})'#11个字符(Youtube视频ID的长度)。
。 - #([\w]?!); #拒绝超长ID。
preg_match($ pattern,$ url,$ matches);
return(isset($ matches [1]))? $ matches [1]:false;
}


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 the Youtube I.D itself compatible with all the possible youtube link formats. Not exclusively beginning with youtube.com.

For example:

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

is there a clever strategy I can use to match the video I.D -wtIMTCHWuI compatible with all these formats. I'm thinking character counting and matching = ? / . & characters.

解决方案

I had to deal with this for a PHP class I wrote a few weeks ago and ended up with a regex that matches any kind of strings: With or without URL scheme, with or without subdomain, youtube.com URL strings, youtu.be URL strings and dealing with all kind of parameter sorting. You can check it out at GitHub or simply copy and paste the code block below:

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

Test cases: https://3v4l.org/GEDT0
JavaScript version: https://stackoverflow.com/a/10315969/624466

To explain the regex, here's a split up version:

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

这篇关于Youtube I.D解析新的URL格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆