我有一个简单的清理函数,它在我在某处读到的 foreach 语句中嵌套了一个 switch 语句是一种不好的做法,但是我无法想出更好的解决方案,我的代码如下,任何帮助都是赞赏...
public static function DB_Sanitize($input, $santype = 'SQL', $cleanKeys = FALSE) {
$type = strtoupper($santype);
if (!is_array($input)) {
$input = array($input);
}
foreach ($input as $key => $value) {
switch ($type) {
case 'SQL':
if ($cleanKeys) {
$key = $this->_mysqli->escape_string($key);
}
$value = $this->_mysqli->escape_string($value);
$clean[$key] = $value;
break;
case 'HTML':
if ($cleanKeys) {
$key = htmlentities($key);
}
$value = htmlentities($value);
$clean[$key] = $value;
break;
default:
if ($cleanKeys) {
$key = $this->_mysqli->escape_string($key);
}
$value = $this->_mysqli->escape_string($value);
$clean[$key] = $value;
break;
}
return $clean;
}