Php iframe到youtube

Php iframe到youtube,php,amp-html,Php,Amp Html,我想把iframe改成amp-youtube。我做了,但有个问题。我使用youtube视频配置,但youtube只需要视频短码。所以我正在第2行中解决这个问题。但第二条线不起作用 $this->html = str_replace("//www.youtube.com/embed/","",$this->html); $this->html = str_replace("?rel=0&showinfo=0","",$this->html); // This is

我想把iframe改成amp-youtube。我做了,但有个问题。我使用youtube视频配置,但youtube只需要视频短码。所以我正在第2行中解决这个问题。但第二条线不起作用

$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);
$this->html=str\u replace(“//www.youtube.com/embed/”,“”,$this->html);
$this->html=str_replace(“?rel=0&showinfo=0”,”,$this->html);//这不起作用

$this->html=preg_replace('/您可以通过以下方式实现:

<?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>';
?>

它工作正常,但仅适用于第一个视频。我有3个$content视频,但它只获取第一个视频的yt id。请循环它。通过iframe在$content中嵌入所有视频

下面就是一个例子

$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>';
$content\u description='Lorem Ipsum只是印刷和排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,而Lorem Ipsum只是印刷和排版行业的虚拟文本。自15世纪以来,当安",;
preg\u match('/embed\/([\w+\-+]+)[\“\?]/”,$content\u description,$match);
$yt='';
echo$yt;
结果

    <amp-youtube
    data-videoid="V6LP8m4GpVc"
    layout="responsive"
    width="480" height="270"></amp-youtube>


我也想回显所有内容。我正在将现有的php网站转换为amp。在同一地点将iframe转换为amp youtube。并将img转换为amp img。

str_replace行工作完全正常-只要输入实际如图所示。猜测:您的变量可能已经(正确地)包含了编码的HTML,因此
&
实际上是
&;
,因此您也需要在str\u replace的搜索参数中使用后者。第一行中的
$match
是什么,您能解释一下吗?
$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>