kmaid Posted February 2, 2009 Share Posted February 2, 2009 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 More sharing options...
MadTechie Posted February 2, 2009 Share Posted February 2, 2009 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; } ?> Link to comment https://forums.phpfreaks.com/topic/143452-solved-validating-2d-and-3d-arrays/#findComment-752487 Share on other sites More sharing options...
printf Posted February 2, 2009 Share Posted February 2, 2009 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 https://forums.phpfreaks.com/topic/143452-solved-validating-2d-and-3d-arrays/#findComment-752489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.