如果字符串包含特定值,则从 preg_match_all 中排除字符串

Exclude string from preg_match_all if string contains certain value

我正在使用 preg_match_all 获取所有脚本并将其放在正文结尾之前,如下所示:

preg_match_all('#(<script.*?</script>)#is', $html, $matches);
$js = '';
foreach ($matches[0] as $value):
    $js .= $value;
endforeach;
$html = preg_replace('#(<script.*?</script>)#is', '', $html);
$html = preg_replace('#</body>#',$js.'</body>',$html);

这破坏了页面上的一些功能,但是对于如下几个脚本:

<script data-template="bundle-summary" type="text/x-magento-template">
      <li>
            <strong class="label"><%- data._label_ %>:</strong>
            <div data-container="options"></div>
      </li>
</script>

我如何使用 preg_match_all 排除 <script data-template 脚本被移动。

我想我可以通过执行以下操作来检查脚本 x-magento-template 脚本:

if (strpos($value, 'type="text/x-magento-template"') === false) {
    $js .= $value;
}

然后它不会被添加到 $js 变量但是我不确定如何停止在下面的行中删除相同的脚本:

$html = preg_replace('#(<script.*?</script>)#is', '', $html);

我需要替换所有脚本,但如果它们包含 type="text/x-magento-template

更新

我做了以下操作,但想知道是否有更有效的方法 preg_match_all?

            preg_match_all('#(<script.*?</script>)#is', $html, $matches);
            $js = '';
            foreach ($matches[0] as $value):
                if (strpos($value, 'type="text/x-magento-template"') === false) {
                    $js .= $value;
                    $html = str_replace($value, '', $html);
                }
            endforeach;
            //$html = preg_replace('#(<script.*?</script>)#is', '', $html);
            $html = preg_replace('#</body>#',$js.'</body>',$html);

计时后,方法与 if 语句之间的差异与非 if 语句之间的差异可以忽略不计,每个时间约为 0.005 秒,所以我很乐意离开它。

对于 html 编辑,DOM 方法可提供更好的结果:

$dom = new DOMDocument;
$state = libxml_use_internal_errors(true);
$dom->loadHTML($html); // or $dom->loadHTMLFile('./file.html'); 

$removeList=[];
$bodyNode = $dom->getElementsByTagName('body')->item(0);

foreach ($dom->getElementsByTagName('script') as $scriptNode) {
    if ( $scriptNode->hasAttribute('type') && $scriptNode->getAttribute('type')=='text/x-magento-template' )
        continue;

    $removeList[] = $scriptNode;
}

foreach ($removeList as $scriptNode) {
    $bodyNode->appendChild($scriptNode);
}

libxml_use_internal_errors($state);

echo $dom->saveHTML();

使用此代码,您不必删除脚本节点,因为它们从 dom 树中的当前位置移动到 body 元素的末尾(因为它们是附加的)。