Home >Backend Development >PHP Tutorial >PHP character filtering method_PHP tutorial
/**
*Character filter
* $santype 1: Strip HTML, XML and PHP tags,
* 2: Strip HTML, XML and PHP tags, convert characters into HTML entities, encode double quotes and single quotes
* 3: Strip HTML, XML and PHP tags, add backslash before specified predefined characters Single quote (') Double quote (") Backslash () NULL
* 4: Used to clean data retrieved from databases or HTML forms (stripping HTML, XML and PHP tags)
* 6: Add a backslash before the specified predefined character Single quote (') Double quote (") Backslash () NULL
*
**/
function sanitize($var, $santype = 1, $allowable_tags = ''){
if ($santype == 1) {return strip_tags($var, $allowable_tags = '');}
elseif ($santype == 2) {return htmlentities(strip_tags($var, $allowable_tags),ENT_QUOTES,'UTF-8');}
elseif ($santype == 3) {
return addslashes(strip_tags($var, $allowable_tags));
}
elseif ($santype == 4) {
return stripslashes(preg_replace('/<([^>]+)>/es', "'<'.sanitize('\1',5).'>'",strip_tags($var, $allowable_tags)));
}
elseif ($santype == 5) {
return preg_replace('/sonw+s*=/is','',$var);
}
elseif ($santype == 6 ){
return addslashes($var);
}
}
摘自 adamboy