2

I have tried to strip the code below

<iframe width="560" height="315" src="//www.youtube.com/embed/QUVdnzC19oU" frameborder="0" allowfullscreen></iframe>

its working but not stripping the links properly all i get is

http://img.youtube.com/vi/www.youtube.comembedQUVdnzC19oU/0.jpg 

instead of

http://img.youtube.com/vi/QUVdnzC19oU/0.jpg

Please find the php code below.

if(preg_match_all('@<iframe\s[^>]*src=[\"|\']([^\"\'\>]+)[^>].*?</iframe>@siu', $item->introtext, $iframesrc) >0){
            if(isset($iframesrc[1])){
                $vid = str_replace(
                    array(
                        'http://youtu.be/',
                        'http://www.youtube.com/embed/',
                        'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);

                $vid = preg_replace('@\/.*?@i', '', $vid);

                if(!(empty($vid))){
                    $result = '' . 
                        '<div class="vimage">
                            <a class="video-link vlink" href="'.$item->link.'" title="">
                                <img src="http://img.youtube.com/vi/'.$vid.'/0.jpg" />
                                <span class="play-icon">&nbsp;</span>
                            </a>
                        </div>';

                    $item->introtext = str_replace($iframesrc['0'], '', $item->introtext);
                }
            }
        }

Thanks in advance.

2 Answers 2

4

Replace

array(
    'http://youtu.be/',
    'http://www.youtube.com/embed/',
    'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);

by

array(
    '//youtu.be/',
    '//www.youtube.com/embed/',
    '//youtube.googleapis.com/v/',
    'http:', 'https:'), '', $iframesrc[1][0]);

The problem is that you URL das not start with http:// as your code expects. My solution will work with //, http:// and https://.

Also the following line is not useful, as it will never have any effect.

$vid = preg_replace('@\/.*?@i', '', $vid);
Sign up to request clarification or add additional context in comments.

Comments

2
<?php
  $url = "www.youtube.com/embed/QUVdnzC19oU";
  $urltokens = explode("/", $url);
  $vid_id = $urltokens[2];
  echo "http://img.youtube.com/vi/" . $vid_id . "/0.jpg";
?>

Considering you don't have http:// in the middle, this code will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.