16

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.

Run
  • 54,938
  • 169
  • 450
  • 748
  • How are you receiving it in the first place, and checking whether it's valid? – Cole Jul 08 '11 at 01:59
  • I do not like none of the answers posted here, I'd prefer a method pinging the url and analyzing the response, I guess if it can be done this way. – Zac Jul 12 '18 at 06:01

8 Answers8

32

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)
Stoosh
  • 2,408
  • 2
  • 18
  • 24
21

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;

}
ampedwebdev
  • 381
  • 3
  • 6
16

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.

Icode4food
  • 8,504
  • 16
  • 61
  • 93
  • 1
    Why is this answer better than parse_url answer? – mgPePe Oct 13 '11 at 08:49
  • @Icode4food - does strpos work for any case of the characters? i.e. "youtube = YOUTUBE" ? – Shackrock Mar 07 '12 at 12:17
  • 5
    This should not be the accepted answer. It fails to return "unknown" for URLs that contain "youtube" or "vimeo" in other parts of the URL. E.g. "http://foo.com/youtube/123" – nickh Mar 09 '13 at 16:04
  • 1
    It won't detect short urls – Mojtaba Aug 20 '15 at 17:36
  • Wrong answer. Try to put the below url http://stackoverflow.com/questions/27969062/regex-to-find-youtube-link-in-string. This will find as a youtube link but it is not – Hashan Jan 19 '16 at 03:43
  • Adding a dot youtube. or vimeo. to be sur that is't not in a url path.like mywebsite.com/youtube/my-youtube-videos.html – Gino Nov 03 '16 at 04:41
3

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";
}
AJ.
  • 27,586
  • 18
  • 84
  • 94
3

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
    }
Cole
  • 1,483
  • 1
  • 11
  • 20
1

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
}
Dũng IT
  • 2,751
  • 30
  • 29
0

Use regular expressions. What language are you using?

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

<?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
?>
theRealWorld
  • 1,188
  • 2
  • 8
  • 23
-1

You can try this

<?php
  if (strpos($videourl, 'youtube') > 0) {
    echo 'This is a youtube video';
  } 
?>
Deepak
  • 185
  • 2
  • 9
  • That sounds pretty wrong. What if `$videourl` contains this anywhere else? Like `this-is-not-youtube.com` – Nico Haase Oct 18 '21 at 18:13
  • It will work in this case as well. Because it will check the position of "youtube" in the whole string and if it found the word youtube in the string then it will return the position of that string and will return the true result. in your example the position of the youtube word is 12 i.e. greater than 0. So, it will echo that message (This is a youtube video). You can do the same for vimeo as well. – Deepak Oct 19 '21 at 07:29
  • But the URL `this-is-not-youtube.com` should not return "This is a youtube video", should it? – Nico Haase Oct 19 '21 at 07:32
  • Yes, in that case parse_url function can be used to get the hostname to match it exactly. – Deepak Oct 19 '21 at 07:42