Jump to content

[SOLVED] Sanitizing array using strip_tags and htmlentities


allinurl

Recommended Posts

Hello. Im sanitizing an array of $_POST values with the function sanitize_values(), what Im trying to do now is to sanitize the array but instead of doing the way Im doing it

I would like to do it using strip_tags and htmlentities. What could be the best way of doing this using those functions mentioned before. Thanks in advance

 

function sanitize_values($value)
{
$search = array( '&', '\\', '<', '>', '[', ']' );
$replace = array( '&', '&#92;', '<', '>', '&#91;', '&#93;' );
$str = str_replace( $search, $replace, $str );
return( $str ); 
}

Link to comment
Share on other sites

function sanitize_values($array)
{
foreach ($array as $k => $v)
{
$array[$k] = htmlentities(strip_tags($v),ENT_QUOTES);
}
return $array;
}

or:

function sanitize_values($val)
{
if (is_array($val))
{
  foreach ($val as $k => $v)
   {
   $val[$k] = htmlentities(strip_tags($v),ENT_QUOTES);
   }
  }
else
  {
  $val = htmlentities(strip_tags($val),ENT_QUOTES);
  }
return $val;
}

Link to comment
Share on other sites

Alternatively...

<?php
function clean($var) {
    if(is_array($var)) {
        array_map("clean",$var);
    } else {
        $var = htmlentities(strip_tags($var),ENT_QUOTES);
    }
    return $var;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.