问题描述
这个问题之前已经问过了,我发现这个:
但是我正在寻找稍微不同的东西。
我需要匹配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上
JavaScript版本:
解释正则表达式,这是一个分裂的版本:
/ **
*检查输入的字符串是否为有效的YouTube网址
*,并尝试从中提取YouTube视频ID它。
* @author Stephan Schmitz< [email protected]>
* @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:
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 <[email protected]>
* @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 <[email protected]>
* @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格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!