2011-03-15 27 views
1

我該如何編寫一個正則表達式來檢查這兩個匹配?vimeo視頻的簡單正則表達式

http://vimeo.com/moogaloop.swf?clip_id=CLIP_ID

http://player.vimeo.com/video/CLIP_ID

+1

刮擦是邪惡的。收集您自己的內容。 – 2011-03-15 19:38:34

+2

如何編寫2個表達式,每個表達式一個?另外,你寫過什麼嗎?如果你寫了一個RE,那麼糾正你的RE會容易些。 – Abbas 2011-03-15 19:38:39

+2

@Tomalak - 誰說他在刮?也許他想編寫一個檢測vimeo鏈接的CMS? – Cfreak 2011-03-15 19:41:32

回答

0

未經測試,但應該工作:

preg_match('/http:\/\/(?:player\.)?vimeo\.com\/(?:moogaloop\.swf\?clip_id=|video\/)/',$string) 

其中$string是你對任何匹配。

2

此正則表達式將兩個工作(測試):

preg_match('#http://(\w+.)?vimeo.com/(video/|moogaloop\.swf\?clip_id=)\w+#i', $content); 

更新

爲了捕捉clip_id使用這種調整正則表達式:

preg_match('#http://(?:\w+.)?vimeo.com/(?:video/|moogaloop\.swf\?clip_id=)(\w+)#i', $content, $match); 

$match[1]包含剪貼ID

基本上在每個'()'內添加'?:'表明它不會顯示在$ match數組中。

+0

這一項工作,但是搜尋匹配是這樣的: 陣列 ( [0] =>陣列 ( [0] => HTTP://www.vimeo .com/moogaloop.swf?clip_id = 1018674 [1] => http://www.vimeo.com/moogaloop.swf?clip_id=1018674 ) [1] =>陣列 ( [0] => www。 [1] => www。 ) [2] =>數組 ( [0] => moogaloop.swf?clip_id = [1] => moogaloop.swf?clip_id = ) ) – 2011-03-15 19:51:25

+0

是沒有什麼辦法可以捕獲clip_id? – 2011-03-15 19:52:10

+1

當然有。查看更新。 – RDL 2011-03-15 20:14:13

1
function getVimeoInfo($link) 
{ 
    if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $link, $match)) 
    { 
     $id = $match[1]; 
    } 
    else 
    { 
     $id = substr($link,10,strlen($link)); 
    } 

    if (!function_exists('curl_init')) die('CURL is not installed!'); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    $output = unserialize(curl_exec($ch)); 
    $output = $output[0]; 
    curl_close($ch); 
    return $output; 
}