douqiang4245 2013-09-07 18:30
浏览 33
已采纳

使用php从嵌入代码生成youtube缩略图

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条回答 默认 最新

  • duanjia4097 2013-09-07 18:42
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格