どこかで読んだ 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;
}