php解析网址与正则表达式

时间:2013-10-07 22:49:31

标签: php regex

我有一个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 个答案:

答案 0 :(得分: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 - Extract video ID

答案 1 :(得分:1)

请参阅How to find all Youtube video ids in a string using a regex?

// 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;
}

答案 2 :(得分:1)

这可能不是正则表达式的工作,而是适用于您选择的语言的现有工具。正则表达不是一个魔术棒,您可以在遇到涉及字符串的每个问题上挥手。您可能希望使用已编写,测试和调试的现有代码。

在PHP中,使用parse_url函数。

Perl:URI module

Ruby:URI module

.NET:'Uri' class

答案 3 :(得分: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抓取最后一个数组元素

未经测试但应该正常工作