Skip to content

Instantly share code, notes, and snippets.

@Stichoza
Last active December 14, 2015 02:19
Show Gist options
  • Save Stichoza/5013306 to your computer and use it in GitHub Desktop.
Save Stichoza/5013306 to your computer and use it in GitHub Desktop.
Parse POST/GET requests by one simple function.
<?php
function parse_request($vars, $encode_html = false, $return = false) {
$vars = explode(",", $vars);
if ($return) {
foreach ($vars as $value) {
$value = trim($value); // trim extra spaces
$result[$value] = get_var($value, $encode_html);
}
return $result; // returns associative array
} else {
foreach ($vars as $value) {
$value = trim($value); // trim extra spaces
global $$value;
$$value = get_var($value, $encode_html); // global scope variable
}
}
}
function get_var($var, $encode_html = false) {
if (!empty($_REQUEST[$var]) || $_REQUEST[$var] == 0) {
return ($encode_html) ? htmlspecialchars($_REQUEST[$var], ENT_QUOTES) : $_REQUEST[$var];
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment