gpt4 book ai didi

php - 如何从此网址中提取 YouTube ID

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:10 26 4
gpt4 key购买 nike

如何从此网址中提取 YouTube ID?

https%253A%252F%252Fwww.youtube.com%252Fwatch%253Fv%253Dnn5hCEMyE-E

我试过这个正则表达式:

$url = $_GET["q"];
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);

最佳答案

首先解码它并使用数组匹配的第二个索引([1]):

if(isset($_GET["q"]))
{
$url = urldecode(rawurldecode($_GET["q"]));
# https://www.youtube.com/watch?v=nn5hCEMyE-E
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
echo $matches[1];
# nn5hCEMyE-E
}

关于php - 如何从此网址中提取 YouTube ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186181/

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