为什么下载 youtube 文件 ID 不起作用? [英] Why downloading the youtube file id does not work?

查看:24
本文介绍了为什么下载 youtube 文件 ID 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了从 youtube 站点获取电影 ID 的代码,如果 ID 有连字符-",脚本在 natomast 上运行良好,脚本无法从 url 获取 ID.我在正则表达式方面很弱,但仍然试图转换表达式,但我无法处理它.你能指导我或告诉我我在做什么错误吗?谢谢

I found the code that gets the movie id from the youtube site, the script works well natomast if the ID has a hyphen "-" the script does not get the ID from the url. I'm weak in regular expressions but still tried to convert the expression, but I can not deal with it. Can you direct me or show me what error I'm doing? Thanks

我的代码:

$links = array(
'https://www.youtube.com/watch?v=-SXKV0jDxuA',
'https://www.youtube.com/watch?v=ylfhCpi9AEU'
);
foreach ($links as $link){
    preg_match("#([\/|\?|&]vi?[\/|=]|youtu\.be\/|embed\/)(\w+)#", $link, $matches);
    var_dump(end($matches));
}   //result => ylfhCpi9AEU

推荐答案

至于我将如何改进您的评论模式:

As for how I would improve your comment pattern:

~(?:[/?&]vi?[/=]|youtu\.be/|embed/)\K[\w-]{10,12}~

  • 这使用了一个不同的模式分隔符——一个没有在模式本身中使用的字符.这避免了不必要地转义模式中的字符.
  • 在字符类内部使用管道 (|) 不是字符类的工作方式.字符类 ([..]) 是目标字符或字符范围的列表.通过在字符类中写入 |,您将 | 作为非预期的有效字符包含在内.
  • \w 相当于 [A-Za-z0-9_],因此如果在适当的地方使用它,您的模式会更简洁.
  • \K 开始全字符串匹配,这样您就不需要使用任何捕获组来提取 ID(这可以提高性能并减少输出数组膨胀).
  • 我在 ID 子字符串上使用了范围量词(正如其他 StackOveflow 用户所做的那样),以允许扩展有效 ID 长度.如果我的模式因为 ID 长度大于 12 而过时,只需调整上限即可.
    • This uses a different pattern delimiter -- a character that is not used in the pattern itself. This avoids having to escape characters in the pattern unnecessarily.
    • Using pipes (|) inside of character classes is not how character classes work. Characters classes ([..]) are a list of characters or character ranges that are targeted. By writing | inside the character class, you are including | as a valid character which is not intended.
    • \w is the equivalent of [A-Za-z0-9_], so your pattern is made more brief if use it where appropriate.
    • \K starts the fullstring match so that you don't need to use any capturing groups to extract the ID (this improves performance and reduces the output array bloat).
    • I am using a ranged quantifier on the ID substring (as other StackOveflow users have done) to allow the expansion of the valid ID length. If my pattern become obsolete because of IDs that have a length greater than 12, just adjust the upper limit.
    • 至于我将如何编写我能想到的最具包容性的模式(考虑到我在 StackOverflow 中发现的所有可能的 url 变体):

      As for how I would write the most inclusive pattern I can dream up (given all of the possible url variations that I found laying around StackOverflow):

      *注意,这不会检查您 url 的前面.它假定您只处理有效的 youtube 网址.

      *Note, this doesn't check the front of your url. It assumes that you are only dealing with valid youtube urls.

      ~(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)\K[\w-]{10,12}~
      

      这是一个模式演示,其中包含一个很长的 youtube 网址列表,我成立.(我不会详细说明此模式的所有组成部分,因为这对于您正在处理的 url 来说可能有点过分.如果您或其他任何人希望我将其分解,请询问.)

      This is a pattern demo that includes a long-ish list of youtube urls that I found. (I won't spell out the all of the components of this pattern, because it may be overkill for the urls that you are dealing with. If you or anyone else would like me to break it down, just ask.)

      *完全清楚,我的模式不是设计用于验证 youtube 网址,而是设计用于从有效的 youtube 网址中提取 ID.

      *To be perfectly clear, my patterns are not designed to VALIDATE youtube urls, but rather they are designed to EXTRACT IDs from valid youtube urls.

      当与 preg_match() 一起使用时,您只需要访问第一个元素(全字符串匹配 [0])值.

      When used with preg_match() you will only need to access the first element (fullstring match [0]) value.

      这篇关于为什么下载 youtube 文件 ID 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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