PHP strip_tag和htmlspecialchars不适用于多个输入

问题描述 投票:0回答:3

我正在尝试从多个用户输入中删除html标签。我单独尝试了它,但是当我将它变成一个函数时,它<< [not删除了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; }
php htmlspecialchars strip-tags
3个回答
1
投票
您没有将值分配给任何东西,因此值在内部循环中丢失。

function clean($field) { foreach ($field as $key => $value) { $field[$key] = htmlspecialchars(strip_tags($value)); } return $field; }

您还想在返回时保留清理后的版本:

$test = clean($test);


0
投票
您设置了$ value的值,但是当它超出范围时会丢失它。我认为这是正确的:

function clean($field) { foreach ($field as $key => $value) { $field[$key] = htmlspecialchars(strip_tags($value)); } return $field; }


0
投票
通过$value的选项

通过引用:

function clean($field) { foreach ($field as &$value) { $value = htmlspecialchars(strip_tags($value)); } return $field; }
© www.soinside.com 2019 - 2024. All rights reserved.