3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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['name'] = array( 'hahaha' => 'I am an array', ); $_POST['age'] = '  20 '; $_POST['gender'] = 'male'; $_POST['options'] = array( 'a' => array('hahaha' => 'This is an invalid option'), 'b' => ' hoge hoge ', 'c' => array('hahaha' => 'This is an invalid option'), 'd' => "\x0a\x0a\x0a This is invalid encoding value", "\x0a\x0a\x0a" => 'This is invalid encoding key', ); extract(filter_struct_utf8(INPUT_POST, array( 'name' => '', 'age' => FILTER_STRUCT_FULL_TRIM, 'email' => '', 'gender' => '', 'options' => FILTER_STRUCT_FORCE_ARRAY | FIRTER_STRUCT_TRIM, ))); var_dump($name, $age, $email, $gender, $options);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.3.0rc40.0140.00798.97
7.3.0rc30.0760.00316.66
7.3.0beta10.0060.00313.18
7.3.0alpha10.0070.00712.97
7.2.60.0060.00916.79
7.2.00.0030.00719.62
7.1.200.0060.00615.84
7.1.100.0060.00618.40
7.1.70.0040.00417.52
7.1.60.0030.02119.40
7.1.50.0060.01817.26
7.1.00.0030.03322.44
7.0.200.0070.00316.73
7.0.140.0000.07722.00
7.0.60.0070.04720.09
7.0.50.0130.07717.84
7.0.40.0130.07720.31
7.0.30.0400.04720.30
7.0.20.0370.07720.09
7.0.10.0300.04020.11
7.0.00.0070.04020.16
5.6.280.0070.07021.09
5.6.210.0000.04320.52
5.6.200.0000.04318.15
5.6.190.0030.06720.48
5.6.180.0230.04720.53
5.6.170.0270.07720.45
5.6.160.0070.07020.39
5.6.150.0100.07018.19
5.6.140.0170.07318.21
5.6.130.0100.07318.28
5.6.120.0100.05320.92
5.6.110.0200.07021.04
5.6.100.0270.06721.01
5.6.90.0070.08321.02
5.6.80.0030.08020.43
5.6.70.0370.07320.40
5.5.350.0400.04020.36
5.5.340.0000.09017.96
5.5.330.0000.09020.40
5.5.320.0300.08020.35
5.5.310.0230.05720.34
5.5.300.0070.08017.99
5.5.290.0100.08317.94
5.5.280.0070.08320.90
5.5.270.0030.05020.98
5.5.260.0030.04020.87
5.5.250.0100.03320.71
5.5.240.0170.06020.26
5.4.450.0370.06719.64
5.4.440.0170.04719.39
5.4.430.0200.03019.26
5.4.420.0070.07319.51
5.4.410.0130.04319.39
5.4.400.0130.03718.88
5.4.390.0200.04019.24
5.4.380.0170.03319.09
5.4.370.0170.04019.23
5.4.360.0130.03719.22
5.4.350.0200.03019.15
5.4.340.0130.03719.00
5.4.320.0130.03719.01
5.4.310.0170.04719.32
5.4.300.0170.03319.08
5.4.290.0100.04719.18
5.4.280.0100.04019.21
5.4.270.0230.06319.21
5.4.260.0230.05319.22
5.4.250.0300.06019.30
5.4.240.0170.07719.09
5.4.230.0200.06719.14
5.4.220.0230.04318.97
5.4.210.0200.05719.19
5.4.200.0200.06319.25
5.4.190.0300.06719.18
5.4.180.0370.06318.94
5.4.170.0200.05319.14
5.4.160.0200.06318.83
5.4.150.0200.03719.23
5.4.140.0230.04016.41
5.4.130.0270.06016.40
5.4.120.0300.06016.55
5.4.110.0170.04316.42
5.4.100.0170.03716.61
5.4.90.0270.02716.61
5.4.80.0330.05716.62
5.4.70.0270.07316.48
5.4.60.0330.05716.56
5.4.50.0100.05316.53
5.4.40.0130.05316.19
5.4.30.0330.06316.48
5.4.20.0270.07316.19
5.4.10.0030.05016.54
5.4.00.0100.06015.98
5.3.290.0100.04314.94
5.3.280.0270.05714.55
5.3.270.0300.03314.72
5.3.260.0230.05714.66
5.3.250.0300.03714.64
5.3.240.0170.04314.62
5.3.230.0330.06314.71
5.3.220.0270.04014.65
5.3.210.0200.08014.60
5.3.200.0200.04314.53
5.3.190.0270.06714.50
5.3.180.0230.04014.57
5.3.170.0300.03314.50
5.3.160.0200.05014.60
5.3.150.0230.05014.66
5.3.140.0230.03314.57
5.3.130.0230.04314.74
5.3.120.0270.07014.59
5.3.110.0230.06314.70
5.3.100.0270.06013.99
5.3.90.0270.07013.95
5.3.80.0170.05014.06
5.3.70.0300.06714.13
5.3.60.0270.05313.96
5.3.50.0230.04313.90
5.3.40.0330.05013.89
5.3.30.0270.06714.17
5.3.20.0130.05313.65
5.3.10.0200.05313.55
5.3.00.0300.06313.89
5.2.170.0270.05311.89
5.2.160.0200.05311.89
5.2.150.0230.05311.89
5.2.140.0200.04711.89
5.2.130.0200.03711.89
5.2.120.0200.05711.89
5.2.110.0170.05711.89
5.2.100.0270.05011.89
5.2.90.0130.04011.89
5.2.80.0170.04011.89
5.2.70.0170.03711.89
5.2.60.0230.05711.89
5.2.50.0170.06311.89
5.2.40.0200.05711.89
5.2.30.0170.06011.89
5.2.20.0230.03711.89
5.2.10.0200.04711.89
5.2.00.0230.04011.89
5.1.60.0200.04311.89
5.1.50.0070.03311.89
5.1.40.0170.03311.89
5.1.30.0170.05311.89
5.1.20.0230.03011.89
5.1.10.0130.04011.89
5.1.00.0170.05311.91
5.0.50.0100.03011.94
5.0.40.0100.02711.94
5.0.30.0070.03011.95
5.0.20.0100.04311.96
5.0.10.0070.02311.97
5.0.00.0030.03311.97
4.4.90.0070.03711.89
4.4.80.0100.03011.89
4.4.70.0100.03011.89
4.4.60.0100.03311.89
4.4.50.0130.03011.89
4.4.40.0070.02711.89
4.4.30.0230.02311.89
4.4.20.0100.02311.89
4.4.10.0170.03011.94
4.4.00.0100.03011.94
4.3.110.0100.01311.95
4.3.100.0030.03011.96
4.3.90.0100.01711.96
4.3.80.0030.02711.98
4.3.70.0100.01711.99
4.3.60.0100.01711.99
4.3.50.0070.01712.00
4.3.40.0070.03012.00
4.3.30.0070.02012.00
4.3.20.0030.02712.00
4.3.10.0030.02712.01
4.3.00.0000.02712.01