This has got me completely stumped:
print_r($json);
echo json_encode($json);
output:
Array
(
[query] => dia
[suggestions] => Array
(
[0] => Diana Johnson
[1] => Diane Abbott
)
)
{"query":"dia","suggestions":[null,null]}
What on earth is going wrong?
edit Just to add to the general wtf-ery of this, here's another sample:
Array
(
[query] => david
[suggestions] => Array
(
[0] => David Cameron
[1] => David Amess
[2] => David Anderson
[3] => David Blunkett
[4] => David Burrowes
)
)
{"query":"david","suggestions":["David Cameron",null,null,null,null]}
I'm posting this as an answer because I need the full formatting abilities of the normal answer box.
Yeah, it's UTF-8 all right. From the PHP interactive prompt:
php > $david = urldecode('David%A0Amess');
php > echo json_encode($david);
null
php > $david = urldecode('David%20Amess');
php > echo json_encode($david);
"David Amess"
php > $david = urldecode('David%c2%a0Amess');
php > echo json_encode($david);
"David\u00a0Amess"
So, we can assume that you're dealing with either ISO-8859 or Windows-1252, given that we're dealing with a broken NBSP. We can fix this with iconv:
php > $david = urldecode('David%A0Amess');
php > $david_converted = iconv('Windows-1252', 'UTF-8', $david);
php > echo json_encode($david_converted);
"David\u00a0Amess"
So, this means that you are going to need to not trust what you're pulling out of MySQL, assuming you've done the SET NAMES thing. Clearly something has gone awry when you were inserting data. You probably weren't giving MySQL well-formed UTF-8, and it stupidly did not complain. (If you were using other, smarter, more correct databases, and tried to insert the unencoded NBSP, they would have rejected the input.)
This looks like an autocomplete script. I assume your results are loaded from a database, are you sure they're utf-8? If you cannot replicate this functionality by hardcoding the array, then it's probably an encoding issue.
According to http://php.net/manual/en/function.json-encode.php, "This function only works with UTF-8 encoded data."
You can also use http://php.net/manual/en/function.json-last-error.php to see the last error.
Related
In PHP, I am trying to create an array from a CSV file in this way.
// CSV file:
$csv = array_map('str_getcsv', file("{$uploadPath}/{$myFile}"));
Of course it is working for me, but on thing I have to fixed. If I have non-English language characters in my CSV file, then those characters not showing correctly in PHP. That mean its showing question marks instead of showing original characters.
This is the output of $csv array:
Array
(
[0] => Array
(
[0] => SISP-0002
[1] => Harpic Floral 500ml
[2] => ???????? ??????? 500ml
[3] => 4792037107765
)
)
UPDATE:
I have set charset inside page tag as shown here.
<meta charset="utf-8">
And also I have saved my CSV file with unicode(UTF-8)
Can anybody tell me what would be the possible workaround to fix this issue.
This appears to be the definitive question and answers for UTF-8 and php UTF-8 all the way through
i got an array from echo $posts
Array
(
[0] => Array
(
[id] => 14
[name] => اسطنبوليه
)
)
i have this array (part of the main array)and now i wish to convert it to json form. however i am not able to do so, i tried to convert the data through
echo json_encode ($posts);
but instead of original data i am getting an output u0627u0633u0637u0646u0628u0648u0644u064au0647n
can anyone tell how i can get the correct form
Please try echo json_encode($posts,JSON_UNESCAPED_UNICODE) (php 5.4 and above)
2 things. Firstly view source of the output in the browser and you should see it as a JSON encoded string rather than the interpreted version.
Secondly it looks like there is some character encoding issues as the string you're getting back is unicode. Make sure you have the encoding set right on your server and browser.
I'm trying to json_encode a php array however the "choices" part is returning null.
Array ( [indexer] => 7 [section] => 1 [question] => What does the last paragraph indicate about an optimistic perspective? [answer] => a [choices] => There has never been a culture or religion that is genuinely optimistic`It is “a riddle of the universe”`No enlightened culture sees the world in a truly optimistic manner`Optimistic perspectives are only held by the weak )
{"indexer":"7","section":"1","question":"What does the last paragraph indicate about an optimistic perspective?","answer":"a","choices":null}
any ideas why?
Edit: full code:
$check = mysqli_query($con, "select * from questions where section = '$section' and indexer = '$question'");
$result = mysqli_fetch_array($check, MYSQLI_ASSOC);
print_r($result);
echo json_encode($result);
json_encode expects all content that you're encoding to be valid UTF-8. If any part of the content isn't proper UTF-8, json_encode will fail silently for that part (b
All string data must be UTF-8 encoded.
My guess is that the “ marks isn't properly UTF-8 encoded (but originates from a CP1252 based encoding).
See json_last_error to retrieve the last error code from the json module.
While working with the cakephp, I found an issue mentioned below.
Fetched the encrypted field info from DB (encrypted using Security::rijndael)
Passed this whole data as an array format to the custom Library(Own created lib).
When i echoed the data in lib as well in controller I amazed to see the result. The value (encrypted one) are showing blank in the lib. Is I missed anything in codding? I searched on google but didn't get the satisfactory answer, Please help me out. Your help will really be appreciated.
Here is result i am getting in controller and Library respectively
Array
(
[0] => Array
(
[value] => s�i�(�RTf���cBЉF� | �r�n#ô�
)
)
Array
(
[0] => Array
(
[value] =>
)
)
Check your character encoding; a place I worked at ran into a similar issue and it was due to our db trying to encode characters it did not support. UTF-8 generic is a, well, generic encoding type.
In order to pass array variables via my curl script, I am using serialize because curl POST elements must not be arrays.
The string that I get after serialization is:
a:10:{s:8:"question";s:18:"How are you doing?";s:11:"view_option";s:6:"select";s:10:"txt_answer";a:4:{i:0;s:8:"dsadsdsa";i:1;s:5:"dsads";i:2;s:10:"dsadsdsdsa";i:3;s:0:"";}s:4:"next";s:1:"9";s:7:"bgimage";s:0:"";s:9:"bck_color";s:0:"";s:12:"border_color";s:0:"";s:11:"select_font";s:1:"1";s:9:"font_size";s:4:"12px";s:4:"poll";s:9:"Get Poll!";}
Curl makes it:
a:10:{s:8:\"question\";s:18:\"How are you doing?\";s:11:\"view_option\";s:6:\"select\";s:10:\"txt_answer\";a:4:{i:0;s:8:\"dsadsdsa\";i:1;s:5:\"dsads\";i:2;s:10:\"dsadsdsdsa\";i:3;s:0:\"\";}s:4:\"next\";s:1:\"9\";s:7:\"bgimage\";s:0:\"\";s:9:\"bck_color\";s:0:\"\";s:12:\"border_color\";s:0:\"\";s:11:\"select_font\";s:1:\"1\";s:9:\"font_size\";s:4:\"12px\";s:4:\"poll\";s:9:\"Get Poll!\";}
before sending to the server. Above is what I see at the server end. Now, because of the backslashes, above is not unserializable.
What do I do now? If I just unescape all quotes - how do I distinguish between escapes put by CURL and escapes that could be part of the data?
EDIT
The error I get when trying to unserialize the escaped string is:
Notice: unserialize() [function.unserialize]: Error at offset 304 of 351 bytes in /var/www/localserver/test/ser.php on line 8
thanks
JP
Your server probably has magic quotes enabled, which means that your input data is escaped.
Your options are to disable it in your php.ini file or to call stripslashes on the data when it is received.
Escapes that are part of the data will be double escaped, so unescaping them shouldn't be a problem.
Disabling in php.ini
magic_quotes_gpc = Off
stripslashes
$data = stripslashes($_POST['data']);
As I mentioned in my comments, you may want to try JSON instead. But, wanted to point out that this works fine for me.
<?php
$c = "a:10:{s:8:\"question\";s:18:\"How are you doing?\";s:11:\"view_option\";s:6:\"select\";s:10:\"txt_answer\";a:4:{i:0;s:8:\"dsadsdsa\";i:1;s:5:\"dsads\";i:2;s:10:\"dsadsdsdsa\";i:3;s:0:\"\";}s:4:\"next\";s:1:\"9\";s:7:\"bgimage\";s:0:\"\";s:9:\"bck_color\";s:0:\"\";s:12:\"border_color\";s:0:\"\";s:11:\"select_font\";s:1:\"1\";s:9:\"font_size\";s:4: \"12px\";s:4:\"poll\";s:9:\"Get Poll!\";}";
print_r(unserialize($c));
OUTPUT
Array
(
[question] => How are you doing?
[view_option] => select
[txt_answer] => Array
(
[0] => dsadsdsa
[1] => dsads
[2] => dsadsdsdsa
[3] =>
)
[next] => 9
[bgimage] =>
[bck_color] =>
[border_color] =>
[select_font] => 1
[font_size] => 12px
[poll] => Get Poll!
)
EDIT
As mentioned by #lonesomeday, you probably have php magic quotes turned on on the server receiving this data.