<?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);
preferences:
23.24 ms | 402 KiB | 5 Q