Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I strip all javascript out of an HTML document using PHP?

In my email program I use Tidy to clean up the HTML before I send out the emails. A problem is beginning to persist that if I send a mail fetching the html from a url on the web there may exist some javascript in the document.

I want to clean up this html document even more by stripping out all javascript, embedded, referenced and in any form so that the mail exist only of html.

I want to use php's preg_replace() to strip out all javascript from a mail and I need some help with the best regex because it's not my strongest point i must confess.

like image 757
Etienne Marais Avatar asked Nov 30 '10 12:11

Etienne Marais


1 Answers

echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var); 

As shown here.

like image 200
kyndigs Avatar answered Sep 27 '22 18:09

kyndigs