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
https://forums.phpfreaks.com/topic/143452-solved-validating-2d-and-3d-arrays/
Share on other sites

try this

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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.