你的答案...
您不能只在函数外部调用函数来清理所有函数参数。您必须在函数中执行此操作,如下所示:
function foo(/* Polymorphic */){
// Sanitize the arguments
$arguments = sanitize(function_get_args());
// Proceed with the rest of the function
// $arguments will now be sanitized
}
function sanitize(/* Polymorphic */){
// Get the functions arguments
$arguments = function_get_args();
// If the arguments exist then proceed to sanitizing
if(is_array($arguments)){
foreach($arguments as $key=>$value){
$arguments[$key] = stripslashes($value);
}
}
// Return the sanitized array
return $arguments;
}
注意/* Polymorphic */
评论;这实际上意味着您可以提交任意数量的参数。这里唯一的问题是你不知道哪些参数是哪些,除非你像这样提交它们:
// Call the foo function
foo(array(
'username' => $username,
'name' => $name,
'password' => $password
));
然后在 foo 函数中,您将能够像这样访问参数:
function foo(/* Polymorphic */){
// Sanitize the arguments
$arguments = sanitize(function_get_args());
// You only want the first item in the arguments array so remove the others...
$arguments = $arguments[0];
// Proceed with the rest of the function
echo 'Username: '.$arguments['username']."\n";
echo 'Name: '.$arguments['name']."\n";
echo 'Password: '.$arguments['password'];
}
然而...
如果您出于 MySQL(或任何其他数据库)的目的剥离斜线,那么我强烈建议您查看 MySQLi 或 PDO 库和准备好的语句。如果您使用准备好的语句,则无需进行剥离斜杠的工作。有关更多信息,请参阅以下链接:
http://php.net/manual/en/pdo.prepared-statements.php