doushou5761 2016-01-17 02:37
浏览 27
已采纳

使用PHP查找并删除一些javascript标记,但留下另一个标记

i have html containt some googletag like this :

<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>

<script type='text/javascript'>
googletag.cmd.push(function() {
  googletag.defineSlot('/6176201/Opera_120x600', [120, 600], 'div-gpt-ad-6176201-opera_120x600').addService(googletag.pubads());
  googletag.defineSlot('/6176201/Opera_300x250', [300, 250], 'div-gpt-ad-6176201-opera_300x250').addService(googletag.pubads());
  googletag.defineSlot('/6176201/Opera_728x90', [728, 90], 'div-gpt-ad-6176201-opera_728x90').addService(googletag.pubads());
  googletag.defineSlot('/6176201/Contextuals_Operaweb', [557, 30], 'div-gpt-ad-6176201-contextuals_operaweb').addService(googletag.pubads());
  googletag.pubads().collapseEmptyDivs();
  googletag.pubads().enableSingleRequest();
  googletag.enableServices();
});
</script>

<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_728x90'); });
</script>

<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_120x600'); });
</script>

<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-6176201-opera_300x250'); });
</script>

It place random in html code, i want to find and remove it but do not touch another script tag like google analytics, etc. Just do Google search and look in to a lot of answer but still don't know how to do. Please help.

  • 写回答

2条回答 默认 最新

  • dongwo2222 2016-01-17 04:35
    关注

    Try this one regex:

    /<script type='text\/javascript'(?:(?!<\/?script).
    )+?(
    *.*?)*?(googletag)(
    *.*?)*(?:(?!<\/?script>))*?<\/script>/gi
    

    Test it online

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法