我試圖從TED嵌入代碼中提取視頻縮略圖。爲什麼?那麼,我使用的WordPress主題使用自定義字段來處理視頻,但該字段的縮略圖功能不是爲TED構建的。我試圖重新調整它。從TED嵌入代碼中提取視頻縮略圖
這裏的視頻縮略圖檢索功能(如YouTube和Vimeo的覆蓋):
function woo_get_video_image($embed) {
$video_thumb = '';
/* Let's start by looking for YouTube, then Vimeo */
if (preg_match('/youtube/', $embed)) {
// YouTube - get the video code if this is an embed code (old embed)
preg_match('/youtube\.com\/v\/([\w\-]+)/', $embed, $match);
// YouTube - if old embed returned an empty ID, try capuring the ID from the new iframe embed
if(!isset($match[1]))
preg_match('/youtube\.com\/embed\/([\w\-]+)/', $embed, $match);
// YouTube - if it is not an embed code, get the video code from the youtube URL
if(!isset($match[1]))
preg_match('/v\=(.+)&/',$embed ,$match);
// YouTube - get the corresponding thumbnail images
if(isset($match[1]))
$video_thumb = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
} else if (preg_match('/vimeo/', $embed)) {
// Vimeo - get the video thumbnail
preg_match('#http://player.vimeo.com/video/([0-9]+)#s', $embed, $match);
if (isset($match[1])) {
$video_id = $match[1];
// Try to get a thumbnail from Vimeo
$get_vimeo_thumb = unserialize(file_get_contents_curl('http://vimeo.com/api/v2/video/'. $video_id .'.php'));
$video_thumb = $get_vimeo_thumb[0]['thumbnail_large'];
}
}
// return whichever thumbnail image you would like to retrieve
return $video_thumb;
}
下面是一個典型的TED嵌入:
<iframe
src="http://embed.ted.com/talks/andy_puddicombe_all_it_takes_is_10_mindful_minutes.html"
width="560" height="315"
frameborder="0"
scrolling="no"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
而TED API文檔有沒有什麼幫助所有: http://developer.ted.com/API_Docs
我似乎無法自定義preg_match和/或$ get_vimeo_thumb部分(至少這是我認爲正在發生的事情)。基本上,我正在學習PHP的這一部分,它很顛簸。
如果在'src'屬性具有單引號''',而不是雙引號'「'?正則表達式休息。使用正則表達式來解析HTML文檔是個不錯的主意,請查看DomDocument .HTML不是「常規」的,所以REgex不應該是一個選項。 –