Jump to content

[SOLVED] Validating 2D and 3D arrays


kmaid

Recommended Posts

Hi

 

I am making a validation function which will strip any dangerus code from an array or string. The problem is when i create a 2D array it screws up

 

 

    function cleanInput($Data) 
{
	$Data = mysql_real_escape_string(stripslashes($Data));
	$search = array(
		'@<script[^>]*?>.*?</script>@si',   // Strip out javascript
		'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		'@<![\s\S]*?--[ \t\n\r]*>@');         // Strip multi-line comments
	return preg_replace($search, '', $Data);
}

function libStripInputSlashes($Data)
{
    if (is_array($Data)) 
	{
        foreach($Data as $var=>$val) 
	{
            $output[$var] = cleanInput($val);
        }
    }
    else 
	{
       return cleanInput($Data);
    }
    return $output;
}

$Data = libStripInputSlashes(array(array('<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT','<HTML> \' REALINPUT'),array('<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT', '<HTML> \' REALINPUT','<HTML> \' REALINPUT')));
Echo $Data; 

 

 

Any ideas how I could fix this?

 

Thanks

Kmaid

Link to comment
Share on other sites

This will handle deep array structures without using recursion...

 

 

function libStripInputSlashes ( $global )
{
while ( list ( $n, $v ) = each ( $global ) )
{
	foreach ( $v AS $name => $value )
	{
		if ( is_array ( $value ) )
		{
			$global[] =& $global[$n][$name];
		}
		else
		{
			$global[$n][$name] = cleanInput ( $value );
		}
	}
}

return $global;
}

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.