2010-01-24 32 views

回答

11

我不會用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 
) 
*/ 

你也許可以識別它基於主機名,在這種情況下的路徑。

+0

尼斯提示,但要注意:現在它只是路徑nd查詢 – Cogicero 2013-02-25 12:19:41

2
if (preg_match ("/\b(?:vimeo|youtube|dailymotion)\.com\b/i", $url)) { 
    echo "It's a video"; 
} 
1

我不知道你是怎麼拿到的網址,但你可能要檢查「監視」,而不是僅僅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 "; 
} 
8

$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'; 

     } 
相關問題