我要把iframe改成amp-youtube。我做了,但有一个问题。我使用youtube视频配置,但amp-youtube只想要视频短码。所以我正在修复第二行中的这个问题,但是第二行不工作
$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?
正确的代码:
<amp-youtube
data-videoid="mGENRKrdoGY"
layout="responsive"
width="480" height="270"></amp-youtube>我的代码是这样打印的。所以视频不能打开
<amp-youtube
data-videoid="mGENRKrdoGY?rel=0&showinfo=0"
layout="responsive"
width="480" height="270"></amp-youtube>还有另一个问题。如果iframe链接不是youtube,我希望将iframe更改为amp-iframe。
发布于 2018-09-27 13:40:34
您可以通过以下方式实现:
<?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>';
?>发布于 2019-02-27 15:47:46
它可以工作,但只适用于第一个视频..我在$content中有3个视频,但它只得到了第一个视频的id。你能让它循环一下吗..要在$content中通过iframe嵌入所有视频...
下面的是示例
$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>'; preg_match('/embed\/([\w+\-+]+)[\"\?]/', $content_description,$match);
$yt = '<amp-youtube
data-videoid="'.$match[1].'"
layout="responsive"
width="480" height="270"></amp-youtube>';
echo $yt;结果
<amp-youtube
data-videoid="V6LP8m4GpVc"
layout="responsive"
width="480" height="270"></amp-youtube>我也想回应所有的内容..我正在转换现有的php网站到amp。在同样的地点把iframe转到amp-youtube。以及img到amp-img。
https://stackoverflow.com/questions/52516874
复制相似问题