4

我有一个 PHP 变量,其中包含两种可能的 URL 之一:

$text = "http://www.youtube.com/v/wUJQPbALd68?version=3&autohide=1&autoplay=1";
$text = " http://www.youtube.com/watch?v=IcrbM1l_BoI 

如何从两种类型的 url 中提取 id?我想我必须使用正则表达式,但我是一个非常新手。

例如在第一个$textwUJQPbALd68和在第二个是IcrbM1l_BoI

非常感谢。

4

4 回答 4

8
/**
 * get youtube video ID from URL
 *
 * @param string $url
 * @return string Youtube video id or FALSE if none found. 
 * @authro hakre
 */
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;
}

Youtube API - 提取视频 ID

于 2013-10-07T22:53:05.670 回答
1

这可能不是正则表达式的工作,而是您选择的语言的现有工具。 正则表达式不是你在碰巧涉及字符串的每个问题上挥舞的魔杖。您可能希望使用已经编写、测试和调试过的现有代码。

在 PHP 中,使用该parse_url函数。

Perl:URI模块

红宝石:URI模块

.NET:“Uri”类

于 2013-10-08T02:03:14.330 回答
1

请参阅如何使用正则表达式在字符串中查找所有 Youtube 视频 ID?

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs($text) {
    $text = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20130823)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube\.com    # or youtube.com followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\-\s]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w.-]*    # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w.-]*        # Consume any URL (query) remainder.
        ~ix', 
        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
        $text);
    return $text;
}
于 2013-10-07T22:54:02.427 回答
0
$text = "http://www.youtube.com/v/wUJQPbALd68?version=3&autohide=1&autoplay=1"
$text_array = explode("/", $text);

//那么 $text_array[1] 等于 wUJQPbALd68

$text = " http://www.youtube.com/watch?v=IcrbM1l_BoI 
$text_array = explode("=", $text);
$id = end($text_array);

end 抓取最后一个数组元素

未经测试,但应该可以工作

于 2013-10-07T22:55:31.293 回答