php trim() 期望参数 1 是字符串,数组在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22917107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
trim() expects parameter 1 to be string, array given in
提问by user27133
I have this PHP function for converts all the html special characters to html entities, UTF-8 compatible.
我有这个 PHP 函数,用于将所有 html 特殊字符转换为 html 实体,UTF-8 兼容。
function safe($input) {
$text = trim($input); //<-- LINE 31
$text = preg_replace("/(\r\n|\n|\r)/", "\n", $text); // cross-platform newlines
$text = preg_replace("/\n\n\n\n+/", "\n", $text); // take care of duplicates
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
$text = stripslashes($text);
$text = str_replace ( "\n", " ", $text );
$text = str_replace ( "\t", " ", $text );
return $text;
}
Now, I check my script using acunetix web vuln scanner and i see this error :
现在,我使用 acunetix web vuln 扫描器检查我的脚本,我看到这个错误:
This page contains an error/warning message that may disclose sensitive information.The message can also contain the location of the file that produced the unhandled exception.
This may be a false positive if the error message is found in documentation pages.
This vulnerability affects /cms/submit.php.
Discovered by: Scripting (Error_Message.script).
Attack details
URL encoded POST input access was set to 2
Error message found:
<b>Warning</b>: trim() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\cms\includes\safefunc.php</b> on line <b>31</b><br />
How do i fix this?
我该如何解决?
回答by AbraCadaver
As others have said, the error is self explanatory and this function is not built to handle arrays. If you need to handle arrays then something like this:
正如其他人所说,错误是不言自明的,并且此函数不是为处理数组而构建的。如果你需要处理数组,那么像这样:
function safe($input) {
if(is_array($input)) {
return array_map('safe', $input);
}
// rest of code
}
回答by Elias Soares
Read the Warning, and your answer is there.
阅读警告,您的答案就在那里。
Trim must receive a string as parameter, not a array. Use
Trim 必须接收一个字符串作为参数,而不是一个数组。用
var_dump($input)
to check your input variable type.
var_dump($input)
检查您的输入变量类型。
Could you show the code that call function safe()?
你能展示调用函数safe()的代码吗?