3v4l.org

run code in 300+ PHP versions simultaneously
'<?php define('FILTER_STRUCT_FORCE_ARRAY', "\0FILTER_STRUCT_FORCE_ARRAY\0"); function filter_struct_utf8($type, array $default) { static $is_recursive_static = false; $is_recursive = $is_recursive_static; if (!$is_recursive) { $types = array( INPUT_GET => $_GET, INPUT_POST => $_POST, INPUT_COOKIE => $_COOKIE, INPUT_REQUEST => $_REQUEST, INPUT_ENV => $_ENV, INPUT_SERVER => $_SERVER, ); $type = (int)$type; if (!isset($types[$type])) { return $default; } $var = $types[$type]; $is_recursive_static = true; } else { $var = $type; } $ret = array(); foreach ($default as $key => $value) { if ($value === FILTER_STRUCT_FORCE_ARRAY) { $ret[$key] = array(); $arr = isset($var[$key]) ? (array)$var[$key] : array(); foreach ($arr as $k => $v) { if (is_string($v) && preg_match('//u', $k) && preg_match('//u', $v)) { $ret[$key][$k] = $v; } } } elseif (!isset($var[$key])) { $ret[$key] = $value; } elseif (is_array($value)) { $ret[$key] = filter_struct_utf8($var[$key], $value); } elseif (is_array($var[$key]) || !preg_match('//u', $var[$key])) { $ret[$key] = $value; } else { $ret[$key] = $var[$key]; } } if (!$is_recursive) { $is_recursive_static = false; } return $ret; } // テスト用として代入($_POST['email']は未定義) $_POST['name'] = array( // 異常な値(本来は文字列で来るはず) 'hahaha' => 'I am an array', ); $_POST['age'] = '20'; // 正常な値 $_POST['gender'] = 'male'; // 正常な値 $_POST['options'] = array( 'b' => 'yes', // 正常な値 'c' => array('hahaha' => 'This is an invalid option'), // 異常な値(本来は配列はネストしないはず) ); // $_POSTの要素を期待する形になるようにシンボルテーブルにインポートする extract(filter_struct_utf8(INPUT_POST, array( 'name' => '', // 異常なときは強制的に空文字列にする 'age' => '', // 異常なときは強制的に空文字列にする 'email' => '', // 異常なときは強制的に空文字列にする 'gender' => '', // 異常なときは強制的に空文字列にする 'options' => FILTER_STRUCT_FORCE_ARRAY, // 強制的に一次元配列にする ))); // 値を見てみる var_dump($name, $age, $email, $gender, $options);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
' Fatal error: Uncaught Error: Undefined constant "INPUT_REQUEST" in /in/TpFUQ:12 Stack trace: #0 /in/TpFUQ(64): filter_struct_utf8(0, Array) #1 {main} thrown in /in/TpFUQ on line 12
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
' Fatal error: Uncaught Error: Undefined constant "INPUT_REQUEST" in /in/TpFUQ:12 Stack trace: #0 /in/TpFUQ(69): filter_struct_utf8(0, Array) #1 {main} thrown in /in/TpFUQ on line 12
Process exited with code 255.
Output for 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
'string(0) "" string(2) "20" string(0) "" string(4) "male" array(1) { ["b"]=> string(3) "yes" }
Output for 5.1.0 - 5.1.6
' Notice: Use of undefined constant INPUT_POST - assumed 'INPUT_POST' in /in/TpFUQ on line 64 Notice: Use of undefined constant INPUT_GET - assumed 'INPUT_GET' in /in/TpFUQ on line 9 Notice: Use of undefined constant INPUT_POST - assumed 'INPUT_POST' in /in/TpFUQ on line 10 Notice: Use of undefined constant INPUT_COOKIE - assumed 'INPUT_COOKIE' in /in/TpFUQ on line 11 Notice: Use of undefined constant INPUT_REQUEST - assumed 'INPUT_REQUEST' in /in/TpFUQ on line 12 Notice: Use of undefined constant INPUT_ENV - assumed 'INPUT_ENV' in /in/TpFUQ on line 13 Notice: Use of undefined constant INPUT_SERVER - assumed 'INPUT_SERVER' in /in/TpFUQ on line 14 string(0) "" string(0) "" string(0) "" string(0) "" string(27) "FILTER_STRUCT_FORCE_ARRAY"
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/TpFUQ on line 4
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/TpFUQ on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/TpFUQ on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'&'' or `T_VARIABLE' or `T_CONST' in /in/TpFUQ on line 4
Process exited with code 255.

preferences:
251.21 ms | 401 KiB | 355 Q