从嵌入式iframe获取YouTube图片

时间:2013-08-30 02:09:53

标签: php regex iframe youtube embed

我在wordpress页面上为youtube嵌入了iframe。

所以我正在重新创建每个嵌入框架,我只需要留下两件事:src和image

我有用于创建src的正则表达式:

function youtube($html) {
    if (strpos($html, "<iframe" ) !== false) {
        $frame = preg_match('/<iframe.*?\/iframe>/i', $html, $match2);

        $count = preg_match('/src=(["\'])(.*?)\1/', $match2[0], $match);

        var_dump($match);
    } else {
        return $html;
    }
}
add_filter('bp_get_activity_content_body', 'youtube', 10);
add_filter('the_excerpt', 'youtube', 10);
add_filter('the_content', 'youtube', 10);

所以$match包含我的src。但是我用图像堆叠,这里是一个iframe (我还删除了在我的选择中不会做太多的脚本部分)

<iframe width="591" height="332" src="http://www.youtube.com/embed/1lws4QPUL7I?feature=oembed" frameborder="0" allowfullscreen="">
#document
<html lang="en" dir="ltr" data-cast-api-enabled="true">
    <head>
        <title>Craig Ferguson   2013 06 20   Lewis Black, Matt Morales - YouTube</title>
        <link rel="canonical" href="http://www.youtube.com/watch?v=1lws4QPUL7I">
        <link id="css-1394124665" class="www-embed-player" rel="stylesheet" href="http://s.ytimg.com/yts/cssbin/www-embed-player-webp-vflC5nNwO.css" data-loaded="true">
    </head>
    <body id="" class="date-20130829 en_US ltr   ytg-old-clearfix site-left-aligned exp-watch7-comment-ui webkit webkit-537" dir="ltr">
        <div id="player" class="full-frame" style="width: 100%; height: 100%; overflow: hidden;">
            <embed name="player1" height="100%" width="100%" id="player1" tabindex="0" type="application/x-shockwave-flash" src="http://s.ytimg.com/yts/swfbin/watch_as3-vfldizVp8.swf" allowscriptaccess="always" allowfullscreen="true" bgcolor="#000000" flashvars="user_display_name=Radikal%20Edward&amp;iurl=http%3A%2F%2Fi1.ytimg.com%2Fvi%2F1lws4QPUL7I%2Fhqdefault.jpg&amp;ldpj=-10&amp;thumbnail_num_shards=1&amp;rel=1&amp;authuser=0&amp;hl=en_US&amp;probably_logged_in=1&amp;iurlsd=http%3A%2F%2Fi1.ytimg.com%2Fvi%2F1lws4QPUL7I%2Fsddefault.jpg&amp;length_seconds=2376&amp;idpj=-2&amp;cr=US&amp;video_id=1lws4QPUL7I&amp;is_html5_mobile_device=false&amp;user_display_image=https%3A%2F%2Flh4.googleusercontent.com%2F-PcXCu3dRJSk%2FAAAAAAAAAAI%2FAAAAAAAAAAA%2FoIo6fJTO2X4%2Fs28-c-k%2Fphoto.jpg&amp;allow_ratings=1&amp;iurlmaxres=http%3A%2F%2Fi1.ytimg.com%2Fvi%2F1lws4QPUL7I%2Fmaxresdefault.jpg&amp;fexp=939106%2C929117%2C929121%2C929906%2C929907%2C929922%2C929127%2C929129%2C929131%2C929930%2C936403%2C925726%2C925720%2C925722%2C925718%2C929917%2C906945%2C929933%2C920302%2C906842%2C913428%2C920605%2C919811%2C904830%2C919373%2C930803%2C908536%2C904122%2C938701%2C936308%2C909549%2C900816%2C912711%2C904494%2C904497%2C900375%2C906001&amp;view_count=260&amp;endscreen_module=http%3A%2F%2Fs.ytimg.com%2Fyts%2Fswfbin%2Fendscreen-vfl2YNgK7.swf&amp;playlist_module=http%3A%2F%2Fs.ytimg.com%2Fyts%2Fswfbin%2Fplaylist_module-vflie7hg5.swf&amp;enablejsapi=1&amp;avg_rating=5&amp;allow_embed=1&amp;el=embedded&amp;sk=puClsWoCKB-tDaeZo-YHXBSb1UkBB1wuC&amp;feature=oembed&amp;share_icons=http%3A%2F%2Fs.ytimg.com%2Fyts%2Fswfbin%2Fsharing-vflF4tO1T.swf&amp;title=Craig%20Ferguson%20%20%202013%2006%2020%20%20%20Lewis%20Black%2C%20Matt...&amp;sendtmp=1&amp;abd=1&amp;eurl=http%3A%2F%2Flocaldollars.ge%2F&amp;playerapiid=player1&amp;framer=http%3A%2F%2Flocaldollars.ge%2F">
        </div>
    </body>
</html>
</iframe>

而且在我的函数的第一部分中,我想检查iframe是否属于youtube(因为我也有不同的)并且我只检查iframe

 if (strpos($html, "<iframe" ) !== false)

我没有提出正则表达式,因为宽度和高度可能会改变

1 个答案:

答案 0 :(得分:2)

您只想使用视频ID。

<强> CODE

以下SO答案解释了如何获取youtube image from the video ID

然后,您可以使用以下PHP代码作为构建自定义html代码的指南。

function youtube($html) {
   $search = '#<iframe.+?src="http://www.youtube.com/embed/([a-zA-Z0-9_-]{11})[^"]*"[^>]+?>[\S\s]+?</iframe>#i';

   // Is it a youtube iframe ?
   if (preg_match($search, $html, $matches)) { // Yes
       $videoID = $matches[1];

       // Recreate your embed iframe here ...
   } else { // No
       return $html;
   }
}
add_filter('bp_get_activity_content_body', 'youtube', 10);
add_filter('the_excerpt', 'youtube', 10);
add_filter('the_content', 'youtube', 10);

<强> REGEXP

您可以在此处查看正则表达式:http://regexr.com?36hho