1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/\s+/"</span><span style="color: #339933">,</span> <span style="color: #0000ff">" "</span><span style="color: #339933">,</span> <span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤多余回车</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<[ ]+/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"<"</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤<__("<"号后面带空格)</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<\!–.*?–>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//注释</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\!.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤DOCTYPE</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?html.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤html标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?br.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤br标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?head.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤head标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?meta.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤meta标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?body.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤body标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?link.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤link标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?form.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤form标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/cookie/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"COOKIE"</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤COOKIE标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(applet.*?)>(.*?)<(\/applet.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤applet标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?applet.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤applet标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(style.*?)>(.*?)<(\/style.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤style标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?style.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤style标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(title.*?)>(.*?)<(\/title.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤title标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?title.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤title标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(object.*?)>(.*?)<(\/object.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤object标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?objec.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤object标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤noframes标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?noframes.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤noframes标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤frame标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?i?frame.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤frame标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(script.*?)>(.*?)<(\/script.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/<(\/?script.*?)>/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">""</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/javascript/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"Javascript"</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/vbscript/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"Vbscript"</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/on([a-z]+)\s*=/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"On<span style="color: #000099;font-weight: bold">\\</span>1="</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签</span>
<span style="color: #000088">$str</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/preg_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">preg_replace</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"/&#/si"</span><span style="color: #339933">,</span><span style="color: #0000ff">"&#"</span><span style="color: #339933">,</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//过滤script标签,</span>
<span style="color: #000000;font-weight: bold">?></span>