16

如何编写函数来检查提供的 URL 是 youtube 还是 vimeo?

例如,我将这两个 URL 作为字符串存储在数据库中,

http://vimeo.com/24456787

http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded

如果 URL 是 youtube,那么我会将 URL 重写为,

http://www.youtube.com/embed/rj18UQjPpGA?rel=0&wmode=transparent

如果 URL 是 vimeo,那么我将把这个 URL 重写为,

http://vimeo.com/moogaloop.swf?clip_id=24456787

谢谢。

4

8 回答 8

32

使用该parse_url功能拆分 URL,然后进行常规检查

$url = 'http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded';
$parsed = parse_url($url);

会给你这个数组

array
  'scheme' => string 'http' (length=4)
  'host' => string 'www.youtube.com' (length=15)
  'path' => string '/watch' (length=6)
  'query' => string 'v=rj18UQjPpGA&feature=player_embedded' (length=37)
于 2011-07-08T01:30:35.480 回答
19

我最近写了这个函数来做到这一点,希望它对某人有用:

    /**
 * [determineVideoUrlType used to determine what kind of url is being submitted here]
 * @param  string $url either a YouTube or Vimeo URL string
 * @return array will return either "youtube","vimeo" or "none" and also the video id from the url
 */

public function determineVideoUrlType($url) {


    $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/';
    $has_match_youtube = preg_match($yt_rx, $url, $yt_matches);


    $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([‌​0-9]{6,11})[?]?.*/';
    $has_match_vimeo = preg_match($vm_rx, $url, $vm_matches);


    //Then we want the video id which is:
    if($has_match_youtube) {
        $video_id = $yt_matches[5]; 
        $type = 'youtube';
    }
    elseif($has_match_vimeo) {
        $video_id = $vm_matches[5];
        $type = 'vimeo';
    }
    else {
        $video_id = 0;
        $type = 'none';
    }


    $data['video_id'] = $video_id;
    $data['video_type'] = $type;

    return $data;

}
于 2017-03-27T13:25:07.693 回答
14

正如其他人在评论中指出的那样,这是一个快速而肮脏的解决方案,不能很好地处理边缘情况。如果 url 包含“youtube”(example.com/youtube),它将返回误报。下面提到parse_url()解决方案是一个更强大的解决方案。


正则表达式适用于这种类型的事情,但通常strpossubstr更快的性能明智。查看 PHP 文档以获取preg_match(). 在示例下面有一个关于这个东西的注释。

这是原型代码:

function videoType($url) {
    if (strpos($url, 'youtube') > 0) {
        return 'youtube';
    } elseif (strpos($url, 'vimeo') > 0) {
        return 'vimeo';
    } else {
        return 'unknown';
    }
}

显然返回一个字符串不是最好的主意,但你明白了。替换您自己的业务逻辑。

于 2011-07-08T01:32:58.017 回答
3

您可以使用preg_match()

$u1="http://vimeo.com/24456787";
$u2="http://www.youtube.com/watch?v=rj18UQjPpGA&feature=player_embedded";

if(preg_match('/http:\/\/(www\.)*vimeo\.com\/.*/',$u1)){
    // do vimeo stuff
    echo "Vimeo URL found!\n";
}

if(preg_match('/http:\/\/(www\.)*youtube\.com\/.*/',$u2)){
    // do youtube stuff
    echo "YouTube URL found!\n";
}
于 2011-07-08T01:27:09.427 回答
3

由于您要做的就是检查字符串是否存在,因此请使用stripos。如果其中没有 youtube.com 或 vimeo.com,则 url 格式不正确,对吗?stripos 也不区分大小写。

if(stripos($url,'youtu')===false){
    //must be vimeo
    } else {
    //is youtube
    }
于 2011-07-08T01:30:03.863 回答
1

你可以试试我的解决方案:

function checkServer( $domains=array(), $url ) {
    foreach ( $domains as $domain ) {
        if ( strpos($url, $domain ) > 0) {
            return true;
        } else {
            return false;
        }
    }
}

采用:

if( checkServer(array("youtube.com","youtu.be"), $url ) ) {
    //is Youtube url
}
elseif( checkServer(array("vimeo.com"), $url ) ) {
    //is Vimeo
}
elseif ( checkServer(array("any domain"), $url ) ) {
    //is Any Domain
}else {
    //unknow domain
}
于 2016-05-06T01:36:26.470 回答
0

使用正则表达式。您使用什么语言?

编辑:注意到你的标签是 php.ini。它会是这样的:

<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.youtube.com/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo {$matches[0]}\n"; //youtube.com
?>
于 2011-07-08T01:24:13.887 回答
-1

你可以试试这个

<?php
  if (strpos($videourl, 'youtube') > 0) {
    echo 'This is a youtube video';
  } 
?>
于 2021-10-01T09:59:45.113 回答