Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any preg_match to check if a url is a youtube/vimeo/dailymotion video link?

What's the best preg_match syntax to check if a url is a video link of youtube/vimeo/ or dailymotion?

maybe if it's hard, then just to check the domain name.

Thanks

like image 771
CodeOverload Avatar asked Jan 24 '10 23:01

CodeOverload


3 Answers

I wouldn't use preg_match() for this. I think parse_url() is probably a better choice. You can pass a URL string into it, and it will break it down into all the subcomponents for you.

I don't know what the specific video URLs for those sites you mentioned look like, but I'm sure you could come up with some identifying criteria for each one that you could use with the results of parse_url() to identify. As an example, here's what the breakdown of a YouTube link might look like:

$res = parse_url("http://www.youtube.com/watch?v=Sv5iEK-IEzw");
print_r($res);

/* outputs: 
Array (
    [scheme] => http
    [host] => www.youtube.com
    [path] => /watch
    [query] => v=Sv5iEK-IEzw
)
*/

You could probably identify it based on the host name and the path in this case.

like image 164
zombat Avatar answered Sep 27 '22 21:09

zombat


$location = 'your url';

if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $location, $vresult)) {

          $type= 'youtube';

      } elseif(preg_match('/http:\/\/(.*?)blip\.tv\/file\/[0-9]+/', $location, $vresult)) {


          $type= 'bliptv';

      } elseif(preg_match('/http:\/\/(.*?)break\.com\/(.*?)\/(.*?)\.html/', $location, $vresult)) {

          $type= 'break';

      } elseif(preg_match('/http:\/\/www\.metacafe\.com\/watch\/(.*?)\/(.*?)\//', $location, $vresult)) {

          $type= 'metacafe';

      } elseif(preg_match('/http:\/\/video\.google\.com\/videoplay\?docid=[^&]+/', $location, $vresult)) {

          $type= 'google';

      } elseif(preg_match('/http:\/\/www\.dailymotion\.com\/video\/+/', $location, $vresult)) {

          $type= 'dailymotion';

      }
like image 27
chetanspeed511987 Avatar answered Sep 27 '22 21:09

chetanspeed511987


if (preg_match ("/\b(?:vimeo|youtube|dailymotion)\.com\b/i", $url)) {
   echo "It's a video";
}
like image 38
mopoke Avatar answered Sep 27 '22 23:09

mopoke