Skip to content

Instantly share code, notes, and snippets.

@jef-sure
Created February 27, 2021 15:35
Show Gist options
  • Save jef-sure/d3c1edf81ae942acaf370d35b0ca9ece to your computer and use it in GitHub Desktop.
Save jef-sure/d3c1edf81ae942acaf370d35b0ca9ece to your computer and use it in GitHub Desktop.
sub _php_jquery_param {
my ($root, $path, $value) = @_;
my @struct = $path =~ /\[?([^\[\]]+)\]?/g;
return if !@struct;
my $sl = \$root;
for my $key (@struct) {
if ($key =~ /^\d+$/) {
$$sl //= [];
$sl = \$$sl->[$key];
} else {
$$sl //= {};
$sl = \$$sl->{$key};
}
}
if (substr($path, -2, 2) eq '[]') {
push @$$sl, $value;
$sl = \$$sl->[-1];
} else {
$$sl = $value;
}
return $sl;
}
sub _parse_urlencoded {
my $query = $_[0];
my $form = {};
my @pairs = split(/[&;]/, $query);
foreach my $pair (@pairs) {
my ($name, $value) = map {
tr/+/ /;
s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
eval {decode_utf8 $_}
}
split(/=/, $pair, 2);
if (index($name, "[") >= 0 && index($name, "]") > 0) {
_php_jquery_param($form, $name, $value);
} else {
$form->{$name} = $value if defined $name and $name ne '';
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment