来自嵌入代码或使用 PHP 正则表达式 RegEx 的 URL 的 YouTube Vimeo 视频 ID

发布于 2024-10-18 22:18:56 字数 71 浏览 1 评论 0 原文

我想通过其嵌入代码或 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安静被遗忘 2024-10-25 22:18:56

您可以使用 preg_match 来获取 ID。我稍后将在本答案中介绍表达式本身,但以下是如何使用 preg_match 的基本思想:

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

以下是您询问的每种可能输入类型的表达式的细分。我为每个测试用例和结果提供了一个链接。

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

    <前><代码>v=(.{11})

  2. YouTube 嵌入代码可以如下所示(剪掉一些无关的内容) :

    <对象宽度=“640”高度=“390”>
        <参数名称=“电影”值=“http://www.youtube.com/v/89OpN_277yY?fs=...
        ...
    
    

    或者像这样:

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

    <前><代码>vimeo\.com\/([0-9]{1,10})

  4. Vimeo embed 代码采用以下形式:


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

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. For YouTube URLs such as http://www.youtube.com/watch?v=89OpN_277yY, you could use this expression:

    v=(.{11})
    
  2. 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>
    

    Or like this:

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

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

    \/v\/(.{11})|\/embed\/(.{11})
    
  3. 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})
    
  4. Vimeo embed code takes this form:

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

    So you could use this expression:

    player\.vimeo\.com\/video\/([0-9]{1,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.

蓝颜夕 2024-10-25 22:18:56

这里还有另一个类似的[仅适用于 youtube] 的答案,看看可能会有用。
PHP 正则表达式获取 YouTube 视频 ID?

There is another similar [for youtube only] answered here, might be useful to have a look.
PHP Regex to get youtube video ID?

泪之魂 2024-10-25 22:18:56

还有一个问题有很多很好的答案,特别是来自@user2200660。

https://stackoverflow.com/a/16841070/3850405

我更喜欢使用可以处理嵌入代码或当前使用的任何 URL。

http://vimeo.com/423630
https://vimeo.com/1399176
http://vimeo.com/423630087
https://vimeo.com/423630087
https://player.vimeo.com/video/423630087
https://player.vimeo.com/video/423630087?title=0&byline=0&portrait=0
https://vimeo.com/channels/staffpicks/423630087
https://vimeo.com/showcase/7008490/video/407943692

可以处理我遇到的所有网址的正则表达式:

(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*

https://regex101.com/r/p2Kldc/ 1/

$vimeo = 'http://player.vimeo.com/video/67019023';

if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*/", $vimeo, $output_array)) {
    echo "Vimeo ID: $output_array[6]";
}

感谢 @zeckdude 提供 PHP 中的原始示例代码。

https://stackoverflow.com/a/29860052/3850405

JavaScript 版本:

https://stackoverflow.com/a/65846269/3850405

There is another question with a lot of good answers, specifically from @user2200660.

https://stackoverflow.com/a/16841070/3850405

I prefer to use a regex that both can handle embedded code or any URL currently used.

http://vimeo.com/423630
https://vimeo.com/1399176
http://vimeo.com/423630087
https://vimeo.com/423630087
https://player.vimeo.com/video/423630087
https://player.vimeo.com/video/423630087?title=0&byline=0&portrait=0
https://vimeo.com/channels/staffpicks/423630087
https://vimeo.com/showcase/7008490/video/407943692

Regex that can handle all URLs I have come across:

(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*

https://regex101.com/r/p2Kldc/1/

$vimeo = 'http://player.vimeo.com/video/67019023';

if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*/", $vimeo, $output_array)) {
    echo "Vimeo ID: $output_array[6]";
}

Credits to @zeckdude for the original example code in PHP.

https://stackoverflow.com/a/29860052/3850405

JavaScript version:

https://stackoverflow.com/a/65846269/3850405

微暖i 2024-10-25 22:18:56

根据最新的 youtube 标准,您可以使用以下代码从嵌入代码中提取视频 id。

$embed = '<iframe src="//www.youtube.com/embed/n6WYu-pSXH8" frameborder="0"></iframe>';
preg_match('~embed/(.*?)"~', $embed, $output);
echo $output[1];

谢谢

According to the latest youtube standard, you can use following code to extract video id from embed code.

$embed = '<iframe src="//www.youtube.com/embed/n6WYu-pSXH8" frameborder="0"></iframe>';
preg_match('~embed/(.*?)"~', $embed, $output);
echo $output[1];

Thank you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文