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
.
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()
.
This is because parse_str in meant to be used on query strings and whenever there is space in string, it is converted into + so parse_str converts the + to space.
you can pass ths string through str_replace to undo this
$old_string = "a=123&b=456&Signature=aaaa/bbb+i8=&Satus=bbbb"; $string = parse_str($old_string); $new_string = str_replace('+', ' ', $string);
If you face any issues then lemme know
You shoud elaborate you given code use for what. Is it for HTTP Request (like curl post method) or Request URL GET Method.
If its a url, then try use either you replace those char using str_replace method or use urlencode method.
Please refer to these manual: http://php.net/manual/en/function.urlencode.php and http://php.net/manual/en/function.parse-str.php
Read this too: http://php.net/manual/en/function.http-build-query.php
<?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']);
?>