【问题标题】:Extract YouTube Image From String从字符串中提取 YouTube 图像
【发布时间】:2015-02-03 11:46:07
【问题描述】:
【问题讨论】:
标签:
php
regex
iframe
youtube
【解决方案2】:
试试这个代码,它会给你视频图像的网址。
$url = "http://www.youtube.com/embed/ATYklyGZU4I?feature=oembed";
echo getYouTubeVideoImage($url);
function getYouTubeVideoImage($youtube_code){
preg_match('/youtube\.com\/v\/([\w\-]+)/', $youtube_code, $match);
if(!isset($match[1]) || $match[1] == ''){
preg_match('/youtube\.com\/embed\/([\w\-]+)/', $youtube_code, $match);
}
if(!isset($match[1]) || $match[1] == ''){
preg_match('/v\=(.+)&/',$youtube_code ,$match);
}
$full_size_thumbnail_image = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
$small_thumbnail_image1 = "http://img.youtube.com/vi/".$match[1]."/1.jpg";
$small_thumbnail_image2 = "http://img.youtube.com/vi/".$match[1]."/2.jpg";
$small_thumbnail_image3 = "http://img.youtube.com/vi/".$match[1]."/3.jpg";
// return whichever thumbnail image you would like to retrieve
return $full_size_thumbnail_image;
}
【解决方案3】:
只需调用具有所需格式的<img> 元素,例如 php:
$id = "ATYklyGZU4I";
echo "<img src=http://img.youtube.com/vi/$id/1.jpg>";
您的链接中提到了其他格式。