[英]How can I get rid of all JavaScript from an HTML page?
I could use regex to get rid of the <script>
tags in the HTML like this 我可以使用正则表达式来摆脱HTML中的<script>
标记,例如
$html = preg_replace('#<script(.*?)>(.*?)</script>#is','', $html);
So that works fine, but what about inline JavaScript? 这样就可以了,但是内联JavaScript呢? I figured out I could do it this way 我想我可以这样
$nodes = $dom->getElementsByTagName('*');
foreach($nodes as $node)
{
if ($node->hasAttribute('onload')){
$node->removeAttribute('onload');
}
}
The issue with this is I'd have to find all the attributes, and keep making if
statements. 问题是我必须找到所有属性,并继续制作if
语句。 I've also seen libraries, but I want to keep things small. 我也看过图书馆,但我想缩小规模。 So is there any quick way? 有什么快速的方法吗? Also any nice lists with inline attributes if I have to keep doing what I'm doing? 如果我必须继续做我正在做的事情,还有任何带有内联属性的漂亮列表吗?
我会说,不要重新发明轮子,使用http://htmlpurifier.org/之类的库来完成此任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.