PHP:如何检查 URL 是 Youtube 的还是 vimeo 的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6618967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:52:17  来源:igfitidea点击:

PHP: How to check whether the URL is Youtube's or vimeo's

phpurlyoutubevimeo

提问by laukok

How can I write a function to check whether the provided URLs is youtube or vimeo?

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

For instance, I have this two URLs that I store in a database as strings,

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

http://vimeo.com/24456787

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

If the URL is youtube then I will rewrite the URL to,

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

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

If the URL is vimeo then I will rewrite this URL to,

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

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

Thanks.

谢谢。

回答by Stoosh

Use the parse_urlfunction to split the URL up and then just do your normal checks

使用该parse_url函数将 URL 拆分,然后进行常规检查

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

Will give you this array

会给你这个数组

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)

回答by Icode4food

As others have noted in the comments, this is a quick and dirty solutionthat does not handle edge cases well. If the url contains "youtube"(example.com/youtube) it will return a false positive. The parse_url()solution mentioned belowis a much more robust solution.

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



Regular expressionswork well for this type of thing, but often strposor substrare faster performance wise. Check out the PHP documentation for preg_match(). Below the examples there is a note for exactly this thing.

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

Here is prototype code:

这是原型代码:

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

Obviously returning a string isn't the best idea, but you get the point. Substitute your own business logic.

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

回答by AmpedWebDesign

I recently wrote this function to do exactly this, hopefully it's useful to someone:

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

    /**
 * [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;

}

回答by Ronald De Ruiter

Surprising no one has mentioned that youtube urls can also contain the domain 'youtu.be' in which case all of the above solutions would have to be adapted to cater for that eventuality.

令人惊讶的是,没有人提到 youtube 网址也可以包含域“youtu.be”,在这种情况下,必须对上述所有解决方案进行调整以适应这种情况。

回答by AJ.

You can use preg_match():

您可以使用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";
}

回答by Cole

Since all you want to do is check for the presence of a string, use stripos. If it doesn't have youtube.com or vimeo.com in it, the url is malformed, right? stripos is case insensitive, too.

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

if(stripos($url,'youtu')===false){
    //must be vimeo
    } else {
    //is youtube
    }

回答by IT Vlogs

You can try my solution:

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

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

Use:

用:

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
}

回答by theRealWorld

Use regular expressions. What language are you using?

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

Edit: noticed your tag was php. It would be something like this:

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

<?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
?>