首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php iframe to amp-youtube

Php iframe to amp-youtube
EN

Stack Overflow用户
提问于 2018-09-26 19:38:56
回答 2查看 1K关注 0票数 0

我要把iframe改成amp-youtube。我做了,但有一个问题。我使用youtube视频配置,但amp-youtube只想要视频短码。所以我正在修复第二行中的这个问题,但是第二行不工作

代码语言:javascript
复制
$this->html = str_replace("//www.youtube.com/embed/","",$this->html);
$this->html = str_replace("?rel=0&showinfo=0","",$this->html); // This is not working
$this->html = preg_replace('/<iframe\s+.*?\s+src=(".*?").*?<\/iframe>/', '<amp-youtube data-videoid=$1 layout="responsive" width="480" height="270"></amp-youtube>', $this->html);

第2行str_replace无法正常工作。我怎么才能用另一种方式呢?示例preg_replace?

正确的代码:

代码语言:javascript
复制
<amp-youtube
    data-videoid="mGENRKrdoGY"
    layout="responsive"
    width="480" height="270"></amp-youtube>

我的代码是这样打印的。所以视频不能打开

代码语言:javascript
复制
<amp-youtube
        data-videoid="mGENRKrdoGY?rel=0&showinfo=0"
        layout="responsive"
        width="480" height="270"></amp-youtube>

还有另一个问题。如果iframe链接不是youtube,我希望将iframe更改为amp-iframe。

EN

回答 2

Stack Overflow用户

发布于 2018-09-27 13:40:34

您可以通过以下方式实现:

代码语言:javascript
复制
<?php
$iframeCode = '<iframe width="100%" height="450" src="https://www.youtube.com/embed/mGENRKrdoGY?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>';
preg_match('/embed\/([\w+\-+]+)[\"\?]/', $iframeCode,$match); 
echo '<amp-youtube
    data-videoid="'.$match[1].'"
    layout="responsive"
    width="480" height="270"></amp-youtube>';
?>
票数 0
EN

Stack Overflow用户

发布于 2019-02-27 15:47:46

它可以工作,但只适用于第一个视频..我在$content中有3个视频,但它只得到了第一个视频的id。你能让它循环一下吗..要在$content中通过iframe嵌入所有视频...

下面的是示例

代码语言:javascript
复制
$content_description = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an <iframe src="https://www.youtube.com/embed/V6LP8m4GpVc" allowfullscreen="" frameborder="0"></iframe>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an <div class="embed-container"><iframe src="https://www.youtube.com/embed/xfLQcsP0L18" allowfullscreen="" frameborder="0"></iframe></div>';
代码语言:javascript
复制
    preg_match('/embed\/([\w+\-+]+)[\"\?]/', $content_description,$match); 
    $yt = '<amp-youtube
    data-videoid="'.$match[1].'"
    layout="responsive"
    width="480" height="270"></amp-youtube>';

    echo $yt;

结果

代码语言:javascript
复制
    <amp-youtube
    data-videoid="V6LP8m4GpVc"
    layout="responsive"
    width="480" height="270"></amp-youtube>

我也想回应所有的内容..我正在转换现有的php网站到amp。在同样的地点把iframe转到amp-youtube。以及img到amp-img。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52516874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档