1

I have this embed iframe for youtube on my wordpress page.

So I'm recreating every embed frame and I need to left only with two things: src and image

I have regexp for geting 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);

So $match contains my src. but I stack with image, here is an iframe ( I also deleted script parts which in my opition will not do much )

<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>

And also in the first part of my function, I want to check if the iframe belongs to youtube (because I have also different ones) and I only check for iframe

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

I didn't come up with regexp because width and height might change

Stephan
  • 41,764
  • 65
  • 238
  • 329
CBeTJlu4ok
  • 1,072
  • 4
  • 18
  • 51

1 Answers1

2

You want to use the video ID only.

CODE

The following SO answer explains how to get the youtube image from the video ID.

Then you can use the following PHP code as a guide for building your custom html code.

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

You can view the regexp in action here: http://regexr.com?36hho

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329