Strange encoding issue in PHP - php

I have been banging my head in the wall for couple of hours, I still couldn't figure out the issue. I have an associative array and when I try to build a query with it, It shows weird characters in the browser.
$reportVars = [
"__report" => "alpha",
"start_date" => "2001",
"end_date" => "2002",
"dsp_id" => "SPP",
"current_sp_id" => "SPP_1",
"sp_name" => "fawzan"
];
print_r(http_build_query($reportVars));
This is the output I get in the browser
__report=alpha&start_date=2001&end_date=2002&dsp_id=SPP¤t_sp_id=SPP_1&sp_name=fawzan
Note the strange character (¤) in the output after SPP, Before you ask No, I did not copy it from anywhere. I just typed it with my bare hand.
Can anyone please help me here?

&curren is being converted to ¤
you may have few options now:
move the current_sp_id to top, making it the first variable so that there is no & before it
use &amp as separator instead of & only by using:
print_r(http_build_query($reportVars, '', '& amp;'));
(remove the space between & and amp, added it because it's being converted to & only here too).
P.S. php isn't causing this issue as per my understanding, it's how your browser treats &curren by probably converting it to ¤ itself

Related

http_build_query not giving valid string

$requestParams = [ 'aame_uuid' => 'aba627', 'currency' => 'TEST'];
ksort($requestParams);
$hashString = http_build_query($requestParams);
var_dump( $hashString);
gives
string(30) "aame_uuid=aba627¤cy=TEST"
so why does currency transformed to ¤cy , how can correct it ?
I think the problem is the &curren html code.
http://character-code.com/currency-html-codes.php &curren gets replaced to this char.
Maybe you should add an specific seperator like &
to the http_build_query to make sure it will replaced to foo.php?aaaa_uid=aba627&currency=Test
I just tested your code and I get the following string back:
C:\wamp64\www\test\test.php:8:string 'aame_uuid=aba627&currency=TEST' (length=30)
It seems to be working fine for me. Have you tried using some other key? If it gives somewhat the same result your file or server could be corrupt.
If not the word curren might be presaved as a function to do something (which is highly unlikely). Did you mistype it as currentcy maybe (current is a php function)?

Hidden "space" breaks PHP Object property name after retrieving SQL result

I was having an issue where I could get an object back from a Codeigniter query, run print_r($object) and it would show:
stdClass Object ( [client_id] => 4105 [name] => William Hilliard ...
However, when I tried to print $object->name, nothing printed to the screen. Using Codeigniter's result_array() function to retrieve the result fixed the ability to print the value, but still did not explain why an Object property would not print to the screen. Even then, I wanted an object, not an array. Eventually, I went to the SQL I was running:
SELECT
t.client_id as client_id
,CONCAT_WS(' ',t.first,t.last) as name
,t.foo as bar...
When I quoted name as `name` with tildes, the problem was fixed. This made me curious, so I downloaded BBEdit and looked at the line. In every other text editor, there was no space at the end of the line, but deleting and rewriting the line would fix the problem. BBEdit showed something different:
Δ Δ Δ Δ ,CONCAT_WS(' ',t.first,t.last)·as·name ¬
It clearly shows a space between name and ¬. Copying and pasting this
"whitespace" resulted in nothing pasting in text editors and web browsers. However, deleting the character and saving the file in BBEdit would fix the problem in PHP. Can anyone tell me what character this "phantom space" is and a possible way that a developer could have ended up with this garbage in their code?

JSON incomplete variable

for some reason I have a problem getting a JSON input into PHP. Basically, I am importing a variable from a url-encoded JSON, the bit of code I have problem with looks like this:
"nearest_area": [
{
"country": [
{"value": "Czech Republic"}
],
"region": [
{"value": "Moravskoslezsky Kraj" }
]
}
]
When I import and JSON_decode it in PHP, I used the exact same way of getting the two variables. For country I used
data->nearest_area[0]->country[0]->value;
and I got Czech Republic, for the other one I used region instead of country, but for some reason instead of Moravskoslezsky Kraj, I am always getting just the first word - "Moravskoslezsky".
The only reason I could think of what could be causing the problem is encoding. In fact, in Czech, the actual name of the region ("kraj") is "Moravskoslezský". I used the UTF8 decoding procedure and indeed I am getting the proper "ý" at the end instead of "y", but then it just skips the rest....
Any ideas what could be wrong?
OK, sorry I have figured it out, there was a problem with encoding the actual url.

Why would whitespace cause a fatal error in PHP?

I have an associative array that, for all intents and purposes, appears to have absolutely no reason to throw even a warning. The entirety of the source code is as follows:
<?php
$entries = array(
array('date' => '2012-08-12', 'change' => '-19'),
array('date' => '2012-08-13', 'change' => '-21'), 
array('date' => '2012-08-14', 'change' => '-19'),
array('date' => '2012-08-15', 'change' => '-17'),
);
foreach ($entries as $entry) {
print $entry['date'] . ': ' . $entry['change'] . '<br/>';
}
To me everything looks fine, however when I go to view the output in my browser, I get the following error message:
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /Applications/MAMP/htdocs/wtf2.php on line 5
I looked a little closer and then discovered that on line 4, there was what appeared to be a trailing space or two (which I didn't even think twice about, at first). However when I copied the whitespace, pasted it into a new document like so (line 2):
<?php
$whitespace = '  ';
print rawurlencode($whitespace);
... and then viewed the output in my browser, this is what I saw:
%C2%A0%20
I would ask, "How did that get there in the first place?" but I don't really think that's a feasible question to answer. So my actual question is this: how does whitespace like that differ from any other whitespace (especially to the point where it causes a fatal error when ran through the PHP interpreter)? And is there a way to prevent this from happening in the future?
PS: I'm running PHP version 5.3.20 (via MAMP Pro on a Mac).
PPS: To clarify, WHEN THE WHITESPACE ITSELF IS DELETED, THE CODE RUNS FINE.
What you have there is a UTF-8 encoded non-breaking space (%C2%A0, aka ), which breaks PHP's parser. It's a known problem with PHP that is marked as "won't fix" because people "might use this character as part of an identifier." See Request #38766 Nonbreaking whitespace breaks parsing.
just banged my head for about an hour going totally insane about this.
Notepad++ doesn't show ANY difference between a regular space and a nbsp.
If you wonder how this character actually got into your code, this is how i got mine :
altgr + space

How to pass apostrophe (') in NuSOAP?

I am trying to pass an argument containing apostrophe character to function call but apparently it is being stripped out. Here's what I am doing:
$input = array(
'kod_modelu' => "187'",
'nr_nadwozia' => '00552889'
);
$result = $client->call('certyfikat_gwarancji_dane_pobierz', $input);
var_dump($result);
And in output I'm getting:
'kod_modelu' => string '187' (length=3)
'nr_nadwozia' => string '00552889' (length=8)
(I am repeating input in output and apostrophe is being stripped out).
How can I solve this?
EDIT: Here's a code to reproduce the issue:
Pastebin
Ok, case closed.
PHP 5.2.6 is the issue (referring to bugs.php.net). Upgrading to the latest version of 5.2.x branch solves the problem.
This might be a long shot, but since SOAP is XML based your quotes must be converted to XML entities somewhere.
I have no experience with nuSOAP, but a single quote's XML entity value is: &apos;.
Refer to the PHP function: htmlspecialchars()

Categories