从字符串中提取YouTube图像

时间:2014-12-05 10:20:22

标签: php regex iframe youtube

所以我的字符串是

<div class="cm-video">
<iframe width="100%" height="100%" src="http://www.youtube.com/embed/ATYklyGZU4I?feature=oembed" frameborder="0" allowfullscreen></iframe>
</div>

我已阅读How do I get a YouTube video thumbnail from the YouTube API?,但不确定如何专门检索图片

3 个答案:

答案 0 :(得分:0)

只需使用所需格式调用<img>元素,例如php:

$id = "ATYklyGZU4I";
echo "<img src=http://img.youtube.com/vi/$id/1.jpg>";

您的链接中提到了其他格式。

答案 1 :(得分:0)

尝试使用此代码,它会为您提供视频图像的网址。

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

答案 2 :(得分:0)

 let

$ videoURL =“https://www.youtube.com/watch?v=MVQOmonaFOg”;

$ videoAPI =“https://www.youtube.com/oembed?format=json&url=”;

$ data = json_decode(file_get_contents($ videoAPI.urlencode($ videoURL)));

的print_r($数据);