3v4l.org

run code in 300+ PHP versions simultaneously
<?php function filter_input_utf8($type, $default = array()) { static $is_recursive_static = false; $is_recursive = $is_recursive_static; if (!$is_recursive_static) { $types = array( INPUT_GET => $_GET, INPUT_POST => $_POST, INPUT_COOKIE => $_COOKIE, INPUT_REQUEST => $_REQUEST, INPUT_ENV => $_ENV, INPUT_SERVER => $_SERVER, ); if (!isset($types[$type])) { return $default; } $var = $types[$type]; $is_recursive_static = true; } else { $var = $type; } $ret = array(); foreach ((array)$default as $key => $value) { if (!isset($var[$key])) { $ret[$key] = $value; } elseif (is_array($value)) { $ret[$key] = filter_input_utf8($var[$key], $value); } elseif (is_array($var[$key]) || !preg_match('//u', $var)) { $ret[$key] = $value; } else { $ret[$key] = $var[$key]; } } if (!$is_recursive) { $is_recursive_static = false; } return $ret; } $_POST = array( 'a' => array( 'b' => 'Invalid Data', ), 'c' => array( 'd' => 'Valid Data', ), 'e' => 'Valid Data', 'f' => 'Unneeded Data', ); $post = filter_input_utf8($_POST, array( 'a' => null, 'c' => array( 'd' => null, ), 'e' => null, )); var_dump($post);

preferences:
40.92 ms | 402 KiB | 5 Q