I am reading some HTML files and returning them upon request, but when they go through my codes I need to filter out some stuff from these HTMLs, everything is working fine and I'm using about 20 or more str_replace
and preg_replace
to do that which works pretty fine.
Until I used this
$responseBody = preg_replace('/<script>((?!<)[\s\S])*googletagmanager\.com[\s\S]*?<\/script>/', '', $responseBody);
which is supposed to remove this part from html
<script>
(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'//www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM');</script>
and it does that very well in a regex tester => http://regexr.com/3e8nb but on the server cpu just goes up and the server halts. why is this happening? and how can I improve this regex?