如何从url获取youtubevideoID
我正在尝试检查url是否是有效的YouTubevideourl并从中获取YouTubevideoID,到目前为止我使用的是一个简单的javascript拆分function以实现此function,但是这有一些小的缺点,因为youtube有多个url。
我一直在查看其他stackoverflow线程,但是它们只支持1个特定的URL,这不是我需要的。
我需要一些匹配所有这些URL的东西:
HTTP(S)://www.youtu.be/videoID
HTTP(S)://www.youtube.com/watch V = VIDEOID
(以及脚本自动检测是否包含youtubevideo的任何其他短URL)
任何可以通过浏览器快速/高效处理的想法都非常感谢!
试试这个:
var url = "..."; var videoid = url.match(/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/); if(videoid != null) { console.log("video id = ",videoid[1]); } else { console.log("The youtube url is not valid."); }
看正则表达式:
/ (?:https?:\/{2})? // Optional protocol, if have, must be http:// or https:// (?:w{3}\.)? // Optional sub-domain, if have, must be www. youtu(?:be)? // The domain. Match 'youtu' and optionally 'be'. \.(?:com|be) // the domain-extension must be .com or .be (?:\/watch\?v=|\/)([^\s&]+) //match the value of 'v' parameter in querystring from 'watch' directory OR after root directory, any non-space value. /
也许您应该查看Youtube API并尝试通过API解析URL来查看是否有办法获取videoID。
看看这篇SOpost: Youtube API – 提取videoID
这可能很快:
var url = 'http://www.youtu.be/543221'; //http://www.youtube.com/watch?v=SNfYz6Yw0W8&feature=g-all-esi would work also var a = url.split("v=")[1]; a = a != undefined ? a : url.split("youtu.be/")[1]; b = a.split("&")[0];
变量c将具有您的ID。 快。 正则表达式更好……但更难阅读。 我修改了我的代码以解决这两个问题。
有太多种类:
- 最新的短格式:http:
http://youtu.be/NLqAF9hrVbY
- iframe:
http://www.youtube.com/embed/NLqAF9hrVbY
:http://www.youtube.com/embed/NLqAF9hrVbY
- iframe(安全):
https://www.youtube.com/embed/NLqAF9hrVbY
:https://www.youtube.com/embed/NLqAF9hrVbY
- 对象参数:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
:http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
fs = 1&http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
en_US - 对象嵌入:
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
:http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
fs = 1&http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
- 观看:
http://www.youtube.com/watch?v=NLqAF9hrVbY
:http://www.youtube.com/watch?v=NLqAF9hrVbY
- 用户:
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
:http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
- ytscreeningroom:
http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
:http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
vhttp://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
NRHVzbJVx8I - any / thing / goes!:
http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
:http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
- any / subdomain / too:
http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
:http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
- 更多参数:
http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
:http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
spDj54kf-vY&featurehttp://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
g-vrec - 查询可能有点:
http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
:http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
spDj54kf-vY&feature = youtu.be (来源: 如何使用正则表达式在字符串中查找所有YouTubevideoID? )
最好的方法是限制输入数据。
祝好运
试试这段代码
var url = "..."; var videoid = url.match((?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})); if(videoid != null) { console.log("video id = ",videoid[1]); } else { console.log("The youtube url is not valid."); }
正则表达式来自YouTubevideoID正则表达式
你可以使用preg_match轻松完成这里的示例:
$url = "http://www.youtube.com/watch?v=YzOt12co4nk&feature=g-vrec"; preg_match('/v=([0-9a-zA-Z]+)/', $url, $matches); $vid = $matches[1];
现在,您将videoID为:$ vid = YzOt12co4nk;