我想知道是否有更简单的方法来做到这一点
$admsquarecmail = @$_POST['squarecmail'];
$admsquarecmail = htmlspecialchars($admsquarecmail, ENT_COMPAT);
$admsquarecsubject = @$_POST['squarecsubject'];
$admsquarecsubject = htmlspecialchars($admsquarecsubject, ENT_COMPAT);
$admsquarectymessage = @$_POST['squarectymessage'];
$admsquarectymessage = htmlspecialchars($admsquarectymessage, ENT_COMPAT);
$admsquarecontagain = @$_POST['squarecontagain'];
$admsquarecontagain = htmlspecialchars($admsquarecontagain, ENT_COMPAT);
想法不是键入
htmlspecialchars($var, ENT_COMPAT);
每次添加变量。
答案 0 :(得分:1)
您可以将htmlspecialchars
应用于$_POST
$arr = array_map("htmlspecialchars", $_POST, array_fill(0, sizeof($_POST), ENT_COMPAT));
的所有元素:
$admsquarecmail = isset($arr['squarecmail']) ? $arr['squarecmail'] : "";
$admsquarecsubject = isset($arr['squarecsubject']) ? $arr['squarecsubject'] : "";
$admsquarectymessage = isset($arr['squarectymessage']) ? $arr['squarectymessage'] : "";
然后:
$_POST
......等等。
您可以逐一将array_map
应用于$_POST
的元素。在此方法中,您不需要除$admsquarecmail = isset($_POST['squarecmail']) ? htmlspecialchars($_POST['squarecmail'], ENT_COMPAT) : "";
$admsquarecsubject = isset($_POST['squarecsubject']) ? htmlspecialchars($_POST['squarecsubject'], ENT_COMPAT) : "";
$admsquarectymessage = isset($_POST['squarectymessage']) ? htmlspecialchars($_POST['squarectymessage'], ENT_COMPAT) : "";
之外的数组:
function obtain_POST_value($key){
if(array_key_exists($key, $_POST)) return htmlspecialchars($_POST[$key], ENT_COMPAT);
return "";
}
......等等。
您可以创建一个小功能,如下所示:
$admsquarecmail = obtain_POST_value('squarecmail');
$admsquarecsubject = obtain_POST_value('squarecsubject');
$admsquarectymessage = obtain_POST_value('squarectymessage');
然后:
{{1}}
......等等。