Reputation: 73
I'm trying to remove html tags from multiple user input. I tried it individually it worked, but when i turned it into a function it's not removing the html tags...
$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;
}
Upvotes: 0
Views: 222
Reputation: 54841
Option with passing $value
by reference:
function clean($field)
{
foreach ($field as &$value) {
$value = htmlspecialchars(strip_tags($value));
}
return $field;
}
Upvotes: 1
Reputation: 52832
You're not assigning the values to anything, so the value is lost inside your inner loop.
function clean($field)
{
foreach ($field as $key => $value) {
$field[$key] = htmlspecialchars(strip_tags($value));
}
return $field;
}
You also want to keep the cleaned version when returning:
$test = clean($test);
Upvotes: 1
Reputation: 397
You're set the value of $value, but lose it, when it comes out of scope.
I think it'll be correct:
function clean($field)
{
foreach ($field as $key => $value) {
$field[$key] = htmlspecialchars(strip_tags($value));
}
return $field;
}
Upvotes: 0