Forum Moderators: coopster
// Usage example:
// parse($_GET, $varOne, $varTwo:is_numeric, $varThree);
function parse($arr=[], ...$vars) {
foreach ($vars as $key) {
// $varTwo:is_numeric will make sure that the value is numeric
// can use any function here, is_numeric() is just an example
list($str, $func) = explode(':', $key, 2);
global $$str;
if (!$func || $func($arr[$str]))
$$str = $arr[$str] ? false;
}
} // parse($_GET, $varOne:FILTER_VALIDATE_INT, $varTwo:FILTER_SANITIZE_NUMBER_INT, $varThree);
function parse($arr=[], ...$vars) {
foreach ($vars as $key) {
list($key, $filter) = explode(':', $key, 2);
$filter ?= 'FILTER_DEFAULT';
global $$key;
$$key = filter_var($arr[$key], constant($filter)) ? false;
}
} function parse($arr=[], ...$vars) {
foreach ($vars as $key) {
list($key, $filter) = explode(':', $key, 2);
global $$key;
$$key = ($filter) ?
filter_var($arr[$key], constant($filter)) ? false :
$arr[$key] ? false;
}
} function parse($arr=[], ...$vars) {
foreach ($vars as $key) {
list($key, $filter) = explode(':', $key, 2);
$filter ?= 'FILTER_DEFAULT';
$data[$key] = $arr[$key];
$args[$key] = constant($filter);
}
$placeholder = filter_var_array($data, $args);
foreach ($placeholder as $key => $val) {
global $$key;
$$key = $val;
}
} But it's about 10 times slower than extract()! On 10,000 iterations my function finishes in 0.0199s, compared to extract() that ends in 0.002s.
global $$key;
const PARAM_TYPE_INT=0;
function parse($parameters_data)
{
foreach($parameters_data as $key=>$type)
{
$parameters=[];
switch($type)
{
case PARAM_TYPE_INT:
if(is_numeric($_GET[$key]??false))
{
$parameters[$key]=(int)$_GET[$key];
}
break;
// other cases...
}
}
return $parameters;
}
$parameters=parse(["test"=>PARAM_TYPE_INT]);
$parameter_test=is_numeric($_GET["test"]??false)?((int)$_GET["test"]):false;
// ...
It's the other way around here. A major source of baddies in UK is USA. :( UK access is usually ok. I've firewalled most of russia and china and use geoip to block a few others.
But as I said, don't waste your time chasing this kind of micro seconds optimizations. Even if you have millions of visitors, this is not going to make a difference. There are certainly lot of more useful things you should focus on.
so if I can make a simple change to shave 20ms then I'll do it
// $arr is the array we're parsing, usually $_GET
// $vars is an associative array, $variable => $filter
// $more is a list of variables with no filter, pushed to a numeric array
function parse($arr, $vars=[], ...$more) {
if (is_string($vars))
$vars = [$vars => false];
if (!empty($more))
$vars += $more;
foreach ($vars as $key => $filter) {
// if there's no $filter then it becomes [0-9]+ => $variable, have
// to modify it to make $variable => false
if ($filter && is_numeric($key)) {
$key = $filter;
$filter = false;
}
// the goal here is to overwrite any pre-existing variables with the
// same name, so make sure you use unique variable names
global $$key;
// $filter will actually print as a number, and if it's not valid then it
// won't be a number. This is MUCH faster to process than validating
// with filter_list()
$$key = is_numeric($filter) ?
filter_var($arr[$key], $filter) :
$arr[$key];
}
} // preferred, with all of the filters in an array and then those without filters next
parse($_GET, ['one' => FILTER_VALIDATE_INT, 'three' => FILTER_SANITIZE_NUMBER_INT, 'four' => FILTER_VALIDATE_INT], 'two', 'five']);
// "two" without a filter is inside of the array, but it's OK
parse($_GET, ['one' => FILTER_VALIDATE_INT, 'two', 'three' => FILTER_SANITIZE_NUMBER_INT, 'four' => FILTER_VALIDATE_INT], 'five');
// filters are totally optional
parse('one', 'two', 'three', 'four', 'five');
// same as above but unnecessarily in an array, but it's OK
parse(['one', 'two', 'three', 'four', 'five']); function parse($arr, $vars=[], ...$more) {
if (is_string($vars))
$vars = [$vars => false];
if (!empty($more))
$vars += $more;
// returns an associative array of all valid filters
$filter_list = array_flip(array_map('filter_id', array_combine(filter_list(), filter_list())));
foreach ($vars as $key => $filter) {
// if there's no $filter then it becomes [0-9]+ => $variable, modify to $variable => false
if ($filter && is_numeric($key)) {
$key = $filter;
$filter = false;
}
global $$key;
$$key = ($filter && isset($filter_list[$filter])) ?
filter_var($arr[$key], $filter) :
$arr[$key];
}
}