我现在运行的代码有这个问题。
我的代码是输入一个URL,当我单击submit时,它会删除所有标记。我用strip_tags来做那个。然后我使用preg_match_all("/((?:\w'|\w|-)+)/", $contents, $words);来创建和排列每个单词。然后,我有一个foreach循环,它将计数所有的单词,然后将它与另一个foreach循环放在一个表中。
例如,问题是。假设我输入了一个具有以下内容的URL:
<html>
<head>
<title>titel1</title>
</head>
<body>
<div id="div1">
<h1 class="class2">
Testpage-h1
</h1>
<p>
Testpage-p
</p>
</div>
<script>
alert('hallo');
document.getElementById('class2');
</script>
</body>
</html>这将使用我的代码响应以下内容:
document 1
getElementById1 1
class2' 1
hallo 1
alert 1
Testpage-h1 1
Testpage-p 1
titel1 1(很抱歉把它放在“代码”中,但它不允许我在其他情况下使用中断,或者将数字放在彼此之间)
我的问题是,它不应该显示<script></script>标记之间的内容,因为这对我来说是没有用的。这件事有解决办法吗?
我试过消毒过滤之类的东西,但这对我没有帮助。
发布于 2014-04-01 09:42:19
可以在计算之前从字符串中删除< script >...< /script >:
$text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);或者来自remove script tag from HTML content的另一个解决方案(更慢,但有时更正确)
$doc = new DOMDocument();
// load the HTML string we want to strip
$doc->loadHTML($html);
// get all the script tags
$script_tags = $doc->getElementsByTagName('script');
$length = $script_tags->length;
// for each tag, remove it from the DOM
for ($i = 0; $i < $length; $i++) {
$script_tags->item($i)->parentNode->removeChild($script_tags->item($i));
}
// get the HTML string back
$no_script_html_string = $doc->saveHTML();https://stackoverflow.com/questions/22781853
复制相似问题