Search code examples
phpparsinggetbase64rawurl

php parse string without decoding


My $_SERVER['QUERY_STRING'] returns this:

route=common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456&param2=test

As we can see that there is base64 encoded string passed with get request. I want to parse route parameter without decoding its value.

I need this one:

common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456

Not this one:

common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1iZWsxeTJ1aVFHQQ==/456

I tried to parse route parameter with parse_str function. But it decoded the route's value.


Solution

  • You could essentially replicate parse_str but without applying urldecode:

    $x = $_SERVER['QUERY_STRING'];
    $y = explode('&', $x);
    
    $qs = [];
    
    foreach($y AS $z) {
        list($key, $val) = explode('=', $z);
        $qs[$key] = $val;
    }
    

    Which should give you

    array(2) {
      ["route"]=> string "common/home/test/aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g%2Fdj1iZWsxeTJ1aVFHQQ%3D%3D/456"
      ["param2"]=> string "test"
    }