3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param integer $type Constant like INPUT_XXX * @param array $default Default structure of the specified super global var. * Following bitmasks are available: * + FILTER_STRUCT_FORCE_ARRAY - Force 1D array. * + FILTER_STRUCT_TRIM - Set trimmed string. * + FILTER_STRUCT_FULL_TRIM - Trim including full-width and no-break space. * @return string The value of the filtered super global var. * @see http://qiita.com/items/c39b9ee695a5c2e74627 */ define('FILTER_STRUCT_FORCE_ARRAY', 1); define('FILTER_STRUCT_TRIM', 2); define('FILTER_STRUCT_FULL_TRIM', 4); 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])) { throw new LogicException('unknown super global var type'); } $var = $types[$type]; $is_recursive_static = true; } else { $var = $type; } $trim_chars = "\\0\x20\x09\x0a\x0d\x0b"; $full_trim_chars = "{$trim_chars}\xc2\xa0\xe3\x80\x80"; $trim_pattern = "/\A[{$trim_chars}]++|[{$trim_chars}]++\z/u"; $full_trim_pattern = "/\A[{$full_trim_chars}]++|[{$full_trim_chars}]++\z/u"; $ret = array(); foreach ($default as $key => $value) { if (is_int($value) && !($value & ( FILTER_STRUCT_FORCE_ARRAY | FILTER_STRUCT_FULL_TRIM | FILTER_STRUCT_TRIM ))) { if (!$is_recursive) { $is_recursive_static = false; } throw new LogicException('unknown bitmask'); } if (is_int($value) && $value & FILTER_STRUCT_FORCE_ARRAY) { $tmp = array(); if (isset($var[$key])) { foreach ((array)$var[$key] as $k => $v) { if (!preg_match('//u', $k)) { continue; } $value &= FILTER_STRUCT_FULL_TRIM | FILTER_STRUCT_TRIM; $tmp += array($k => $value ? $value : ''); } } $value = $tmp; } if (isset($var[$key]) && is_array($value)) { $ret[$key] = filter_struct_utf8($var[$key], $value); } elseif (!isset($var[$key]) || is_array($var[$key])) { $ret[$key] = is_int($value) ? '' : $value; } else { if (!isset($var[$key]) || is_array($var[$key])) { $var[$key] = null; } elseif (is_int($value) && $value & FILTER_STRUCT_FULL_TRIM) { $var[$key] = preg_replace($full_trim_pattern, '', $var[$key]); } elseif (is_int($value) && $value & FILTER_STRUCT_TRIM) { $var[$key] = preg_replace($trim_pattern, '', $var[$key]); } else { $var[$key] = preg_replace('//u', '', $var[$key]); } if ($var[$key] === null) { $var[$key] = is_int($value) ? '' : $value; } $ret[$key] = $var[$key]; } } if (!$is_recursive) { $is_recursive_static = false; } return $ret; } // テスト用として代入($_POST['email']は未定義) $_POST['name'] = array( // 異常な値(本来は文字列で来るはず) 'INVALID' => 'これは無効な値です', ); $_POST['age'] = ' 20 '; // 正常な値(半角スペースが混入) $_POST['gender'] = ' male     '; // 正常な値(半角スペースと全角スペースが混入) $_POST['comment'] = ' はじめまして!!    '; // 正常な値 $_POST['options'] = array( 'a' => 'yes', // 正常な値 'b' => "\x0a\x0a\xff", // 値のエンコーディングが壊れている 'c' => array('INVALID' => 'これは無効な値です'), // 異常な値(本来は文字列で来るはず) "\x0a\x0a\xff" => 'INVALID', // キーのエンコーディングが壊れている ); // $_POSTの要素を期待する形になるようにシンボルテーブルにインポートする extract(filter_struct_utf8(INPUT_POST, array( 'name' => '', // 異常なときは強制的に空文字列にする 'age' => FILTER_STRUCT_FULL_TRIM, // 全角スペースなども含むトリミングを行う 'email' => '', // 異常なときは強制的に空文字列にする 'gender' => '', // トリミングを行う 'comment' => '', // 異常なときは強制的に空文字列にする // 強制的に一次元配列にし、各要素に対して全角スペースなども含むトリミングを行う 'options' => FILTER_STRUCT_FORCE_ARRAY | FILTER_STRUCT_FULL_TRIM, ))); // 値を見てみる 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/VRLgX:24 Stack trace: #0 /in/VRLgX(108): filter_struct_utf8(0, Array) #1 {main} thrown in /in/VRLgX on line 24
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/VRLgX:24 Stack trace: #0 /in/VRLgX(115): filter_struct_utf8(0, Array) #1 {main} thrown in /in/VRLgX on line 24
Process exited with code 255.
Output for 5.2.9 - 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.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
string(0) "" string(2) "20" string(0) "" string(20) " male     " array(3) { ["a"]=> string(3) "yes" ["b"]=> string(0) "" ["c"]=> string(0) "" }
Output for 5.2.0 - 5.2.8
string(0) "" string(2) "20" string(0) "" string(0) "" array(3) { ["a"]=> string(3) "yes" ["b"]=> string(0) "" ["c"]=> string(0) "" }
Output for 5.1.0 - 5.1.6
Notice: Use of undefined constant INPUT_POST - assumed 'INPUT_POST' in /in/VRLgX on line 108 Notice: Use of undefined constant INPUT_GET - assumed 'INPUT_GET' in /in/VRLgX on line 21 Notice: Use of undefined constant INPUT_POST - assumed 'INPUT_POST' in /in/VRLgX on line 22 Notice: Use of undefined constant INPUT_COOKIE - assumed 'INPUT_COOKIE' in /in/VRLgX on line 23 Notice: Use of undefined constant INPUT_REQUEST - assumed 'INPUT_REQUEST' in /in/VRLgX on line 24 Notice: Use of undefined constant INPUT_ENV - assumed 'INPUT_ENV' in /in/VRLgX on line 25 Notice: Use of undefined constant INPUT_SERVER - assumed 'INPUT_SERVER' in /in/VRLgX on line 26 Fatal error: Uncaught exception 'LogicException' with message 'unknown super global var type' in /in/VRLgX:30 Stack trace: #0 /in/VRLgX(116): filter_struct_utf8('INPUT_POST', Array) #1 {main} thrown in /in/VRLgX on line 30
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/VRLgX on line 16
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/VRLgX on line 16
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/VRLgX on line 16
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/VRLgX on line 16
Process exited with code 255.

preferences:
258.7 ms | 401 KiB | 424 Q