什麼是最好的preg_match語法來檢查一個url是否是youtube/vimeo /或dailymotion的視頻鏈接?任何preg_match來檢查一個url是否是youtube/vimeo/dailymotion視頻鏈接?
也許如果它很難,那麼只是檢查域名。
感謝
什麼是最好的preg_match語法來檢查一個url是否是youtube/vimeo /或dailymotion的視頻鏈接?任何preg_match來檢查一個url是否是youtube/vimeo/dailymotion視頻鏈接?
也許如果它很難,那麼只是檢查域名。
感謝
我不會用preg_match()
這一點。我認爲parse_url()可能是一個更好的選擇。您可以將URL字符串傳遞給它,並將它分解爲所有子組件。
我不知道你提到的這些網站的特定視頻的網址的樣子,但我相信你能想出的每一個,你可以用parse_url()
結果用它來識別一些識別標準。作爲一個例子,這裏是一個YouTube鏈接的故障可能是什麼樣子:
$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
)
*/
你也許可以識別它基於主機名,在這種情況下的路徑。
尼斯提示,但要注意:現在它只是路徑nd查詢 – Cogicero 2013-02-25 12:19:41
if (preg_match ("/\b(?:vimeo|youtube|dailymotion)\.com\b/i", $url)) {
echo "It's a video";
}
我不知道你是怎麼拿到的網址,但你可能要檢查「監視」,而不是僅僅www.youtube.com(由於YouTube視頻鏈接通常有路徑的手錶?什麼。
$res = parse_url("http://www.youtube.com/watch?v=Sv5iEK-IEzw");
if (preg_match("/\/watch/" , $res["path"] )){
echo "found video\n ";
}
$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';
}
是結果只是真/假或你需要知道它匹配 – 2010-01-25 00:06:04
只是如果這是真的還是假的 – Ryan 2010-01-25 00:21:41