PHP

Parse Query String Without Mangling

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
PHP's parse_str converts dots/spaces in keys to underscores. This alternative preserves them — important when parsing third-party query strings (e.g., webhook payloads).
PHP
Raw
<?php
function parseQueryRaw(string $query): array {
    $out = [];
    foreach (explode('&', $query) as $pair) {
        if ($pair === '') continue;
        [$k, $v] = array_pad(explode('=', $pair, 2), 2, '');
        $out[urldecode($k)] = urldecode($v);
    }
    return $out;
}

print_r(parseQueryRaw('user.id=42&user.name=Alice&path=/foo'));
// [ "user.id" => "42", "user.name" => "Alice", "path" => "/foo" ]
//
// (parse_str would have mangled "user.id" → "user_id")
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.