我的网站输出所有youtube嵌入视频作为AMP视图中的amp-iframe。他们玩得很好,然而,我从谷歌搜索控制台上注意到了他们的正确语法(要求一个)。
现在,我已经添加了以下代码:
$ampyoutubevideo = str_replace("https://www.youtube.com/embed/","",$base_content);
$html = preg_replace('/<amp-iframe\s+.*?\s+src=("youtube.com*?").*?<\/amp-iframe>/', '<amp-youtube
data-videoid=$1
layout="responsive"
width="480" height="270"></amp-youtube>', $ampyoutubevideo);
echo $html;这工作得很好,但我的视频被显示了两次,因为正在生成,但代码也存在。我确实认为我的正则表达式不正确,我想知道您是否可以帮助我正确地对它进行语法分析。
我的目的是当嵌入的项目只存在于youtube.com源代码时,将其完全替换。
发布于 2018-10-03 23:34:04
在WP的AMP下也有同样的问题。添加代码,返回内容两次,一次使用amp-youtube标记,另一次使用amp-iframe。
我正在添加以下内容:
$ampyoutubevideo = str_replace("https://www.youtube.com/embed/","",$base_content);
$html = preg_replace('/<amp-iframe\s+.*?\s+src=(".*?").*?<\/amp-iframe>/', '<amp-youtube
data-videoid=$1
layout="responsive"
width="480" height="270"></amp-youtube>', $ampyoutubevideo);
echo $html;并获得:
<div class="embed-responsive embed-responsive-16by9">
<amp-youtube data-videoid="zXrdyx43va" layout="responsive" width="480" height="270"></amp-youtube>
</div>
<div class="embed-responsive embed-responsive-16by9">
<amp-iframe class="embed-responsive-item amp-wp-enforced-sizes" src="https://www.youtube.com/embed/zXrdyx43va" width="300" height="150" frameborder="0" allowfullscreen="" sandbox="allow-scripts allow-same-origin" layout="intrinsic"><div placeholder="" class="amp-wp-iframe-placeholder">
</div>
</amp-iframe>如果我不应用你的代码片段,我会得到这样的输出:
<div class="embed-responsive embed-responsive-16by9">
<amp-iframe class="embed-responsive-item amp-wp-enforced-sizes" src="https://www.youtube.com/embed/zXrdyx43va" width="300" height="150" frameborder="0" allowfullscreen="" sandbox="allow-scripts allow-same-origin" layout="intrinsic"><div placeholder="" class="amp-wp-iframe-placeholder">
</div>
</amp-iframe>我非常确定您需要手动preg_replace iframe,但我不是正则表达式专家。
https://stackoverflow.com/questions/52479366
复制相似问题