• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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!

0
Valleriani
Asked:
Valleriani
  • 2
  • 2
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now