2010-03-10 67 views
1

如何獲取YouTube提供的YouTube嵌入代碼並使用正則表達式將其轉換爲有效的FBML代碼,也就是使用fb:swf標記?正則表達式解析YouTube嵌入代碼

迄今爲止正則表達式我已經想出是:

preg_replace('/<object(.*)<\/object>/i', "Whatever I need here...", $str); 

我知道這是跛,但它是我的第一次嘗試。

PS:YouTube嵌入式代碼樣本,

<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/NWHfY_lvKIQ&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NWHfY_lvKIQ&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object> 
+1

應該採取什麼結果是給定的樣本 – codaddict 2010-03-10 12:27:33

+0

我假設你問如何?從嵌入代碼收集視頻網址,然後你做你的事情,並得到你想要的任何東西,對吧? – Marcelo 2010-03-10 12:31:13

+0

http:// stackoverf low.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – 2010-03-10 12:40:29

回答

-2

至於我已經想通,想從嵌入的代碼收集的視頻網址,然後你做你的事,並得到任何你想用它。

假設整個嵌入式代碼中只有兩個URL(源和值),並且URL中沒有引號(至少不是YouTube的),我使用的正則表達式是:

[^(src=")]http.*[^"] 

說明:http不是由precceeded「SRC =「」是得了直到有報價

0

我不得不這樣做只是一天。下面是我寫的,以獲得影片ID的一個字符串/輸入,並把它們放到一個數組的函數(你可以插入到你的嵌入對象):

preg_match_all('/\/vi?\/([A-Za-z0-9\+_-]+)/i', $str_containing_yt_ids, $yt_ids); 

然後

print_r($yt_ids); 

和如果您需要動態地獲得標題:

function get_yt_title($clip_id){ 
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $clip_id; 
    $entry = simplexml_load_file($feedURL); 
    $video= new stdClass; 
    $media = $entry->children('http://search.yahoo.com/mrss/'); 
    $video->title = ucwords(strtolower($media->group->title)); 
    return $video->title; 
}