如何修复 stripslashes() 在也使用数组时期望参数 1 是字符串

我有一个将值传递给电子邮件的 php 表单。该表单具有单个值和数组。我收到以下错误,我无法修复它。


警告:stripslashes() 期望参数 1 是字符串,数组在 /home/...... 第 147 行


如果尝试添加


if(is_array$cleanvalue = stripslashes($value);

第 147 行如下:


#clean fields for repost or display

foreach ($_POST as $key=>$value) {

$cleanvalue = stripslashes($value);

$repostvalue = stripquotes($cleanvalue);

$hiddenhtml .= "<input type=\"hidden\" name=\"$key\" 

value=\"$repostvalue\">\n";

${$key} = $cleanvalue;

}

提交表单后,我收到以下错误,但直到填写整个表单。警告:stripslashes() 期望参数 1 是字符串,数组在 /home/...... 第 147 行


汪汪一只猫
浏览 113回答 1
1回答

慕森王

如果是,您可以尝试在每个值上使用is_array()和映射stripslashes():foreach ($_POST as $key=>$value) {&nbsp; &nbsp; if (is_array($value)) {&nbsp; &nbsp; &nbsp; &nbsp; ${key} = array_map('stripslashes', $value);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $cleanvalue = stripslashes($value);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $repostvalue = stripquotes($cleanvalue);&nbsp; &nbsp; $hiddenhtml .= "<input type=\"hidden\" name=\"$key\" value=\"$repostvalue\">\n";&nbsp; &nbsp; ${$key} = $cleanvalue;}看起来stripquotes()可能是自定义功能?您需要类似地修改它。如果您想再次在 html 中输出它们,您将需要以不同的方式循环您的值,可能是这样的:foreach ($_POST as $key=>$value) {&nbsp; &nbsp; if (is_array($value)) {&nbsp; &nbsp; &nbsp; &nbsp; ${key} = array_map('stripslashes', $value);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $cleanvalue = stripslashes($value);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $repostvalue = stripquotes($cleanvalue);&nbsp; &nbsp; if (is_array($value))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach($repostvalue as $repost) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hiddenhtml .= "<input type=\"hidden\" name=\"$key[]\" value=\"$repost\">\n";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $hiddenhtml .= "<input type=\"hidden\" name=\"$key\" value=\"$repostvalue\">\n";&nbsp; &nbsp; }&nbsp; &nbsp; ${$key} = $cleanvalue;}
打开App,查看更多内容
随时随地看视频慕课网APP