gpt4 book ai didi

php - 从YouTube网址获取视频ID的模式

转载 作者:行者123 更新时间:2023-12-03 05:45:46 25 4
gpt4 key购买 nike

我尝试按照以下模式从YouTube网址获取YouTube ID。似乎适用于普通的youtube URL,

$pattern = 
'%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /&v=/ # or ?feature=youtu.be&v=NXwxHU2Q0bo
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if ($result) {
return $matches[1];
}

但是此URL https://www.youtube.com/watch?feature=youtu.be&v=NXwxHU2Q0bo失败

https://m.youtube.com/watch?v=w7P_F7DGPU0

要从上述URL获取视频ID,需要包含什么模式?
提前致谢

最佳答案

这是解决方案:

^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:
www\. # Optional www subdomain
| m\. # Optional mobile subdomain
)?
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/ # Shortlink
| /embed/ # Either /embed/
| /v/ # or /v/
| /&v=/ # or ?feature=youtu.be&v=NXwxHU2Q0bo
| /watch\?v= # or /watch\?v=
| /watch\?feature=youtu\.be&v= # alternativ link with watch
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$

https://regex101.com/r/LiCquP/2

https://regex101.com/r/LiCquP/3

对于测试:)

关于php - 从YouTube网址获取视频ID的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43000840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com