I want to remove all the scipt-tags from a HTML-page, except those with the word foo
or bar
.
So I came up with this statement:
$content = preg_replace('#<script((?!foo|bar).)*?</script>#is', '', $content);
echo "Last error: " + preg_last_error();
This works on smaller pages. But now I have a page with 30 big script-tags and it doesn't work. The error I get is: PREG_BACKTRACK_LIMIT_ERROR
So I think I need to improve my regex to prevent this error, because this statement works:
$content = preg_replace('#<script.*?</script>#is', '', $content);
But this statement is removing all the script-tags, while I want to keep some of them.
There are solution about increasing the pcre.backtrack_limit
, but I don't want to go that route. There should be a better solution imho.
The thing is that I don't know how to fix this, because the issue is with the regex as far as I can see.
Could you guide me to make the regex better so this error won't occur?