方法一过滤html自定义函数,代码如下:
- function ihtmlspecialchars($string) {
- if(is_array($string)) {
- foreach($string as $key => $val) {
- $string[$key] = ihtmlspecialchars($val);
- }
- } else {
- $string = preg_replace('/&((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/', '&\1',
- str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));
- }
- return $string;
方法二,代码如下:
-
-
-
-
- function htmlreplace($str,$rptype=0)
- {
- $str = stripslashes($str);
- if($rptype==0)
- {
- $str = htmlspecialchars($str);
- }
- else if($rptype==1)
- {
- $str = htmlspecialchars($str);
- $str = str_replace(" ",' ',$str);
- $str = ereg_replace("[rnt ]{1,}",' ',$str);
- }
- else if($rptype==2)
- {
- $str = htmlspecialchars($str);
- $str = str_replace(" ",'',$str);
- $str = ereg_replace("[rnt ]",'',$str);
- }
- else
- {
- $str = ereg_replace("[rnt ]{1,}",' ',$str);
- $str = eregi_replace('script','script',$str);
- $str = eregi_replace("<[/]{0,1}(link|meta|ifr|fra)[^>]*>",'',$str);
- }
- return addslashes($str);
- }
其它方法,php过滤不安全字符函数,代码如下:
- function uh($str)
- {
- $farr = array(
- "/s+/",
- "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isu",
- "/(<[^>]*)on[a-za-z]+s*=([^>]*>)/isu",
- );
- $tarr = array(
- " ",
- "<\1\2\3>",
- "\1\2",
- );
-
- $str = preg_replace($farr,$tarr,$str);
- return $str;
|