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

2019-03-15 11:42发布

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

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

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,

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

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

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

Thanks.

8条回答
SAY GOODBYE
2楼-- · 2019-03-15 12:00

As others have noted in the comments, this is a quick and dirty solution that 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 below is a much more robust solution.


Regular expressions work well for this type of thing, but often strpos or substr are faster performance wise. Check out the PHP documentation for preg_match(). Below the examples there is a note for exactly this thing.

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.

查看更多
一夜七次
3楼-- · 2019-03-15 12:00

You can use 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";
}
查看更多
可以哭但决不认输i
4楼-- · 2019-03-15 12:01

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.

if(stripos($url,'youtu')===false){
    //must be vimeo
    } else {
    //is youtube
    }
查看更多
来,给爷笑一个
5楼-- · 2019-03-15 12:13

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.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-03-15 12:21

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

$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)
查看更多
Luminary・发光体
7楼-- · 2019-03-15 12:21

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
}
查看更多
登录 后发表回答