正则表达式在文本 iframe 中找到并粘贴允许的屏幕属性 PHP



我有文本html:

<p>Is it my awesome text, and down I place my iframe with video, 
withot allowfullscreen attribute</p>
<p><iframe src="site.com/video.ogg" width="500" height="400"></iframe></p>

我如何使用正则表达式粘贴allowfullscreen

<?php preg_replace('iframe', 'allowfullscreen'); ?>

但如果allowfullscreen已经存在,则不要粘贴。

使用此示例中的正则表达式:

<?php
$no_fs = <<<HTML
  <p>Is it my awesome text, and down I place my iframe with video, 
    withot allowfullscreen attribute</p>
  <p><iframe src="site.com/video.ogg" width="500" height="400"></iframe></p>
HTML;
$has_fs = <<<HTML
  <p>Is it my awesome text, and down I place my iframe with video, 
    withot allowfullscreen attribute</p>
  <p><iframe src="site.com/video.ogg" allowfullscreen width="500" height="400"></iframe></p>
HTML;
    echo preg_replace('/(<iframe(?:[^>](?!allowfullscreen))+)>/', '$1 allowfullscreen>', $no_fs);
    echo "<br>";
    echo preg_replace('/(<iframe(?:[^>](?!allowfullscreen))+)>/', '$1 allowfullscreen>', $has_fs);

请参阅此 ideone.com 代码段中的输出。

使用此正则表达式,您不会向已经具有此属性的 iframe 添加allowfullscreen <> 之间的任何位置。

最新更新