从任何 YouTube URL 获取 YouTube 视频 ID 的 RegEx 模式

2023-10-11php开发问题
6

本文介绍了从任何 YouTube URL 获取 YouTube 视频 ID 的 RegEx 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

让我们以这些 URL 为例:

Let's take these URLs as an example:

  1. http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player
  2. http://www.youtube.com/watch?v=8GqqjVXhfMU

此 PHP 函数在第 1 种情况下无法正确获取 ID,但会在第 2 种情况下正确获取 ID.第 1 种情况非常常见,其中任何内容都可能出现在 YouTube ID 后面.

This PHP function will NOT properly obtain the ID in case 1, but will in case 2. Case 1 is very common, where ANYTHING can come behind the YouTube ID.

/**
 * get YouTube video ID from URL
 *
 * @param string $url
 * @return string YouTube video id or FALSE if none found. 
 */
function youtube_id_from_url($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/
          | /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 (false !== $result) {
        return $matches[1];
    }
    return false;
}

我的想法是,必须有一种方法可以让我只查找v=",无论它位于 URL 的哪个位置,然后获取后面的字符.以这种方式,不需要复杂的 RegEx.这是离谱吗?对起点有什么想法吗?

What I'm thinking is that there must be a way where I can just look for the "v=", no matter where it lies in the URL, and take the characters after that. In this manner, no complex RegEx will be needed. Is this off base? Any ideas for starting points?

推荐答案

if (preg_match('/youtube.com/watch?v=([^&?/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtube.com/embed/([^&?/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtube.com/v/([^&?/]+)/', $url, $id)) {
  $values = $id[1];
} else if (preg_match('/youtu.be/([^&?/]+)/', $url, $id)) {
  $values = $id[1];
}
else if (preg_match('/youtube.com/verify_age?next_url=/watch%3Fv%3D([^&?/]+)/', $url, $id)) {
    $values = $id[1];
} else {   
// not an youtube video
}

这是我用来从 youtube 网址中提取 id 的方法.我认为它适用于所有情况.

This is what I use to extract the id from an youtube url. I think it works in all cases.

注意最后 $values = 视频的 id

Note that at the end $values = id of the video

这篇关于从任何 YouTube URL 获取 YouTube 视频 ID 的 RegEx 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17