【问题标题】:pattern to get video id from youtube URL从 youtube URL 获取视频 ID 的模式
【发布时间】:2017-08-17 11:18:12
【问题描述】:

我尝试了以下模式从 youtube URL 获取 youtube id。它似乎适用于普通的 youtube 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/
          | /&v=/       # or ?feature=youtu.be&v=NXwxHU2Q0bo
          | /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 ($result) {
            return $matches[1];
        }

但此 URL https://www.youtube.com/watch?feature=youtu.be&v=NXwxHU2Q0bo 失败 和 https://m.youtube.com/watch?v=w7P_F7DGPU0

从上述 URL 获取视频 ID 需要包含什么模式? 提前致谢

【问题讨论】:

  • 我可能是错的,但你为什么不使用一个只解析 url 并为每个参数提供键值字典的库。然后只获取“v”键的值?
  • 你试过这种模式吗? $pattern = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\?? v?=?([^#\&\?]*).*/;'
  • parse_str( parse_url( $url, PHP_URL_QUERY ), $vars ); 您的视频 ID 为 $vars['v']
  • 谢谢你的这个问题 :) 我也有同样的问题

标签: php regex youtube


【解决方案1】:

解决方法如下:

^# Match any youtube URL
    (?:https?://)?  # Optional scheme. Either http or https
    (?:
        www\. # Optional www subdomain
      | m\.   # Optional mobile subdomain
    )?      
    (?:             # Group host alternatives
      youtu\.be/    # Either youtu.be,
    | youtube\.com  # or youtube.com
      (?:           # Group path alternatives
        /           # Shortlink
      | /embed/     # Either /embed/
      | /v/         # or /v/
      | /&v=/       # or ?feature=youtu.be&v=NXwxHU2Q0bo
      | /watch\?v=  # or /watch\?v=
      | /watch\?feature=youtu\.be&v= # alternativ link with watch
      )             # End path alternatives.
    )               # End host alternatives.
    ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
    $

https://regex101.com/r/LiCquP/2

https://regex101.com/r/LiCquP/3

为了测试:)

【讨论】:

    【解决方案2】:

    另一种选择是使用parse_url()parse_str() 函数。这是一个遍历您提供的两个 URL 的示例:

    <?php
    $youtube_urls = array(
        'https://m.youtube.com/watch?v=w7P_F7DGPU0',
        'https://www.youtube.com/watch?feature=youtu.be&v=NXwxHU2Q0bo'
    );
    
    foreach($youtube_urls as $youtube_url){
        $video_id = NULL;
    
        $parsed_url = parse_url($youtube_url);
    
        if( isset($parsed_url['query']) ){
            parse_str($parsed_url['query'],$query_params);
    
            if( isset($query_params['v']) ){
                $video_id = $query_params['v'];
            }
    
        }
    
        echo $video_id.'<br>';
    }
    

    http://php.net/manual/en/function.parse-url.php

    http://php.net/manual/en/function.parse-str.php

    另外,您可以使用PHP_URL_QUERY 组件作为parse_url() 的第二个参数,它会直接输出查询字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-24
      • 2011-05-02
      • 2012-05-22
      • 2011-03-28
      • 2019-12-07
      • 1970-01-01
      • 2011-07-22
      相关资源
      最近更新 更多