来自嵌入代码或来自带有 PHP 正则表达式 RegEx 的 URL 的 YouTube Vimeo 视频 ID [英] YouTube Vimeo Video ID from Embed Code or From URL with PHP Regular Expression RegEx

查看:30
本文介绍了来自嵌入代码或来自带有 PHP 正则表达式 RegEx 的 URL 的 YouTube Vimeo 视频 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过其嵌入代码或从 URL 获取 YouTube 或 Vimeo 的视频 ID,有什么解决方案可以用 PHP 做到这一点?

I want to get Video ID for YouTube or Vimeo via its Embed code or from URL, Any solution to do this with PHP ?

推荐答案

你可以使用 preg_match 以获取 ID.我将在本答案的后面介绍表达式本身,但这里是如何使用 preg_match 的基本思想:

You could use preg_match to get the IDs. I will cover the expressions themselves later in this answer, but here is the basic idea of how to use preg_match:

preg_match('expression(video_id)', "http://www.your.url.here", $matches);
$video_id = $matches[1];

<小时>

以下是您询问的每种可能输入类型的表达式细目.我在每个链接中都包含了一个链接,其中显示了一些测试用例和结果.


Here is a breakdown of the expressions for each type of possible input you asked about. I included a link for each showing some test cases and the results.

  1. 对于 YouTube 网址,例如 http://www.youtube.com/watch?v=89OpN_277yY,您可以使用 这个表达式:

  1. For YouTube URLs such as http://www.youtube.com/watch?v=89OpN_277yY, you could use this expression:

v=(.{11})

  • YouTube 嵌入代码可能看起来像这样(一些无关的东西被剪掉了):

  • YouTube embed codes can either look like this (some extraneous stuff clipped):

    <object width="640" height="390">
        <param name="movie" value="http://www.youtube.com/v/89OpN_277yY?fs=...
        ...
    </object>
    

    或者像这样:

    <iframe ... src="http://www.youtube.com/embed/89OpN_277yY" ... </iframe>
    

    因此,从任一样式获取 ID 的表达式将是 this:

    So an expression to get the ID from either style would be this:

    \/v\/(.{11})|\/embed\/(.{11})
    

  • 据我所知,
  • Vimeo URL 看起来像 http://vimeo.com/.我发现的最低值只是 http://vimeo.com/2,我不知道是否有上限,但我现在假设它限制为 10 位数字.希望知道细节的人可以纠正我.可以使用这个表达式:

  • Vimeo URLs look like http://vimeo.com/<integer>, as far as I can tell. The lowest I found was simply http://vimeo.com/2, and I don't know if there's an upper limit, but I'll assume for now that it's limited to 10 digits. Hopefully someone can correct me if they are aware of the details. This expression could be used:

    vimeo\.com\/([0-9]{1,10})
    

  • Vimeo 嵌入代码采用以下形式:

    <iframe src="http://player.vimeo.com/video/<integer>" width="400" ...
    

    所以你可以使用这个表达式:

    So you could use this expression:

    player\.vimeo\.com\/video\/([0-9]{1,10})
    

    或者,如果数字的长度最终可能超过 10,您可以使用:

    Alternately, if the length of the numbers may eventually exceed 10, you could use:

    player\.vimeo\.com\/video/([0-9]*)"
    

    请记住,如果您将表达式用双引号括起来," 将需要使用 \ 进行转义.

    Bear in mind that the " will need to be escaped with a \ if you are enclosing the expression in double quotes.

    <小时>

    总而言之,我不确定您想如何实现这一点,但是您可以将所有表达式与 | 组合在一起,也可以单独匹配每个表达式.如果您希望我提供有关如何组合表达式的更多详细信息,请在此答案中添加评论.


    In summary, I'm not sure how you wanted to implement this, but you could either combine all expressions with |, or you could match each one separately. Add a comment to this answer if you want me to provide further details on how to combine the expressions.

    这篇关于来自嵌入代码或来自带有 PHP 正则表达式 RegEx 的 URL 的 YouTube Vimeo 视频 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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