PHP and REGEXP: Remove a specific line that has a certain javascript file in it.

Apparently when using AVG (Antivirus) it likes to add coding in WYSIWYG such as CKEDITOR. Most help has been to 'disable' AVG, but this doesn't suite me for my website. I'd prefer the coding be removed if it was there on save. I already have PHP processing after clicking save, I just don't know how to do this portion because it can be slightly random, but the javascript file seems to always be the same:

This is the line that gets added:
<script src="/A2EB891D63C8/avg_ls_dom.js" type="text/javascript"></script>

The thing is, the 'A2EB891D63C8' can be random from what I see. The script is always avg_ls_dom.s, and it has a starting <script and ending </script> right after it. It's a bit above what I know of, since I only know regexp in basic terms (Like removing all but a-z0-9/etc.)

Any suggestions here? I just simply want to remove it every time in a preg_replace (I believe that's what it would use.)

Thank you very much!

LVL 7
VallerianiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DerokorianCommented:
<?php

// TEST DATA
$sample = <<<TEXT

<html><head>
<script type="text/javascript">function check(var a) { return a; }</script>
<script src="/A2EB891D63C8/avg_ls_dom.js" type="text/javascript"></script>
</head>
<body>
Here is some same text.
</body>
</html>

TEXT;

// PATTERN TO FIND SCRIPT
$pattern = "#<script.*avg_ls_dom.js.*</script>#i";

// REPLACE PATTERN MATCHES WITH NOTHING
$replaced = preg_replace($pattern,'',$sample);

echo '<pre>';

// VISUALIZE ORIGINAL SAMPLE
echo htmlentities($sample);
echo '<br><br><br>';

// VISUALIZE REPLACEMENT
echo htmlentities($replaced);
echo '</pre>';

Open in new window

0
käµfm³d 👽Commented:
$pattern = "#<script.*avg_ls_dom.js.*</script>#i";
That's a very bad idea. The dot-star is greedy, and you can end up replacing more than you intend.

My suggestion would be:

$result = preg_replace('#<script +src=["\']?/[^/]+/avg_ls_dom\.js[^>]+>\s*</script>#i', '', $input);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DerokorianCommented:
Interestingly no matter how many script tags i put on the test data, it only removes the one he specifically asked to be removed. Of course, could just use a negate instead of a dot.
$pattern = "#<script[^>]*avg_ls_dom.js[^>]*></script>#i";
0
käµfm³d 👽Commented:
It does work, but then again all of your <script> tags are on different lines I would bet (and you are not using single-line mode). If you put them on one single line, then you would see a different result.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.