douqiaotong8682 2018-01-15 14:23
浏览 192
已采纳

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

Im using preg_match_all to grab all scripts and place bfore body end like below:

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);

This has broken some functionality on the page however for a few scripts like below:

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

How can i use the preg_match_all to exclude <script data-template scripts from being moved.

I figured i could check if the script x-magento-template script by doing something like below:

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

Then it won't be added to the $js variable however am unsure how to stop the same scripts being deleted in the below line:

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

I need to replace all scripts however not if they contain type="text/x-magento-template

Update

I did the below but am wondering if there is a more efficient way of doing this with 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);

After timing the difference between the method with the if statment and not the differences were negligible with a time of about 0.005 seconds each so am happy to leave it.

  • 写回答

1条回答 默认 最新

  • dongzha3058 2018-01-15 15:13
    关注

    For html editing, a DOM approach gives better results:

    $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();
    

    With this code you don't have to delete script nodes since they move from their current position in dom tree to the end of the body element (since they are appended).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)