我正在尝试从多个用户输入中删除html标记。我单独尝试了一下,它很有效,但当我把它变成一个函数时,它不是删除html标签。。。
$test = array('name' => '<script>alert("HELLO..");</script>',
'phone' => '23497999000000'
);
(clean($test));
function clean($field)
{
foreach ($field as $key => $value) {
$value = htmlspecialchars(strip_tags($value));
}
return $field;
}
您没有将值分配给任何东西,因此该值在内部循环中丢失。
function clean($field)
{
foreach ($field as $key => $value) {
$field[$key] = htmlspecialchars(strip_tags($value));
}
return $field;
}
您还希望在返回时保留已清理的版本:
$test = clean($test);
通过引用传递$value
的选项:
function clean($field)
{
foreach ($field as &$value) {
$value = htmlspecialchars(strip_tags($value));
}
return $field;
}
您设置了$value的值,但当它超出范围时就会丢失。我认为这是正确的:
function clean($field)
{
foreach ($field as $key => $value) {
$field[$key] = htmlspecialchars(strip_tags($value));
}
return $field;
}