Any preg_match to check if the url is for youtube / vimeo / dailymotion?

What is the best preg_match syntax to check if the url is a youtube / vimeo / or dailymotion video image?

perhaps if itโ€™s complicated, then just to check the domain name.

thanks

+7
php regex preg-match
source share
4 answers

I would not use preg_match() for this. I think parse_url () is probably the best choice. You can pass it a string of URLs and it will be broken down into all subcomponents for you.

I donโ€™t know how the specific video URLs look for the sites you talked about, but I'm sure you could find some identification criteria for each of them that you could use with the results of parse_url() for identification, As an example, what a YouTube link breakdown looks 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 can probably determine it based on the hostname and path in this case.

+11
source share

$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'; } 
+8
source share
 if (preg_match ("/\b(?:vimeo|youtube|dailymotion)\.com\b/i", $url)) { echo "It a video"; } 
+2
source share

I donโ€™t know how you will get this URL, but you can check the โ€œwatchโ€ and not just at www.youtube.com (since youtube video links usually have a way to watch?).

 $res = parse_url("http://www.youtube.com/watch?v=Sv5iEK-IEzw"); if ( preg_match( "/\/watch/" , $res["path"] ) ){ echo "found video\n "; } 
+1
source share

All Articles