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
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.
$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';
}
if (preg_match ("/\b(?:vimeo|youtube|dailymotion)\.com\b/i", $url)) {
echo "It's a video";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With