Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php parse_str convert plus(+) to spaces

Tags:

php

I'm using parse_str to get an array from a string, which is like:

$str='a=123&b=456&Signature=aaaa/bbb+i8=&Satus=bbbb';

I noticed that the function parse_str does convert plus to space, but how can I avoid this things.

new edit:
The $str to a value that I get from a string which is like this:

YT0xMjMmYj00NTYmU2lnbmF0dXJlPWFhYWEvYmJiK2k4PSZTYXR1cz1iYmJi

I get the string and make base64_decode,then get the $str.

like image 678
code.g Avatar asked Sep 02 '26 14:09

code.g


2 Answers

That's because parse_str() expects a URL-encoded string as its first argument or, more specifically, a query string which is of mime-type application/x-www-form-urlencoded.

For historical reasons a +-sign, besides its percent-encoded counterpart %20, is also interpreted as a space, in the context of this mime-type. Wikipedia has this to say about this:

The application/x-www-form-urlencoded type


[...] The encoding used by default is based on an early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with + instead of %20. [...]

So, you need to make sure you feed parse_str() a properly URL-encoded string to get the characters that you expect. If you expect a + it should first be properly percent-encoded as %2B, before you pass it to parse_str().

like image 136
Decent Dabbler Avatar answered Sep 05 '26 03:09

Decent Dabbler


<?php
$str='a=123&b=456&Signature=aaaa/bbb+i8=&Satus=bbbb';
$new_string = str_replace('+', '.', $str);
parse_str($new_string,$old_string);
$signature = str_replace('.', '+', $old_string['Signature']);
?>
like image 20
Maulik patel Avatar answered Sep 05 '26 03:09

Maulik patel