PHP json_encode() making data null - php

I have this code here:
case 'resource_list':
if(file_exists('content.php')){
include('../ajax/content.php');
} else {
die('does not exist');
}
$html = render_content_page($array[1],$array[2]);
$slate = 'info_slate';
$reply_array = array(
'html' => $html,
'slate' => $slate
);
echo json_encode($reply_array);
break;
i have debugged every level right up until json_encode() is called. But the data i receive back in my ajax is nul for the html key. This code is essentially a copy and paste of another case the just calls a function other than render_content_page() but that works perfectly fine.
$reply_array var_exports to:
array (
'html' => '<ol>
<li unit="quiz" identifier=""><img src="img/header/notifications.png"/>Fran�ois Vase Volute Krater</li>
</ol>',
'slate' => 'info_slate',
)

My initial thought is that special character in Fran�ois Vase Volute Krater, as json_encode only works with UTF-8 encoded data.
Try UTF-8 encoding it before JSON encoding it like so:
json_encode(utf8_encode("Fran�ois Vase Volute Krater"));

Maybe problem is with encoding?
As manual states, json_encode() works only only with utf8 encoded data:
This function only works with UTF-8 encoded data.
http://php.net/json_encode

As documented, json_encode expects its input text in UTF-8. Most likely, your input (the ç) is not in UTF-8.
Use utf8_encode (if you're currently using ISO-8859-1) or mb_convert_encoding (otherwise) to convert input strings to UTF-8.

Related

PHP json encode - Malformed UTF-8 characters, possibly incorrectly encoded [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 months ago.
I'm using json_encode($data) to an data array and there's a field contains Russian characters.
I used this mb_detect_encoding() to display what encoding it is for that field and it displays UTF-8.
I think the json encode failed due to some bad characters in it like "ра▒". I tried alot of things utf8_encode on the data and it will by pass that error but then the data doesn't look correct anymore.
What can be done with this issue?
The issue happens if there are some non-utf8 characters inside even though most of them are utf8 chars. This will remove any non-utf8 characters and now it works.
$data['name'] = mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');
If you have a multidimensional array to encode in JSON format then you can use below function:
If JSON_ERROR_UTF8 occurred :
$encoded = json_encode( utf8ize( $responseForJS ) );
Below function is used to encode Array data recursively
/* Use it for json_encode some corrupt UTF-8 chars
* useful for = malformed utf-8 characters possibly incorrectly encoded by json_encode
*/
function utf8ize( $mixed ) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} elseif (is_string($mixed)) {
return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
}
return $mixed;
}
Please, make sure to initiate your Pdo object with the charset iso as utf8.
This should fix this problem avoiding any re-utf8izing dance.
$pdo = new PDO("mysql:host=localhost;dbname=mybase;charset=utf8", 'user', 'password');
With php 7.2, two options allow to manage invalid UTF-8 direcly in json_encode :
https://www.php.net/manual/en/function.json-encode
json_encode($text, JSON_INVALID_UTF8_IGNORE);
Or
json_encode($text, JSON_INVALID_UTF8_SUBSTITUTE);
you just add in your pdo connection charset=utf8
like below line of pdo connection:
$pdo = new PDO("mysql:host=localhost;dbname=mybase;charset=utf8", 'user', 'password');
hope this will help you
Remove HTML entities before JSON encoding. I used html_entity_decode() in PHP and the problem was solved
$json = html_entity_decode($source);
$data = json_decode($json,true);
Do you by any chance have UUIDs in your result set? In that case the following database flag will help:
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true
If your data is well encoded in the database for example, make sure to use the mb_ * functions for string handling, before json_encode. Functions like substr or strlen do not work well with utf8mb4 and can cut your text and leave a malformed UTF8
I know this is kind of an old topic, but for me it was what I needed. I just needed to modify the answer 'jayashan perera'.
//...code
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i=0; $i < sizeof($result) ; $i++) {
$tempCnpj = $result[$i]['CNPJ'];
$tempFornecedor = json_encode(html_entity_decode($result[$i]['Nome_fornecedor']),true) ;
$tempData = $result[$i]['efetivado_data'];
$tempNota = $result[$i]['valor_nota'];
$arrResposta[$i] = ["Status"=>"true", "Cnpj"=>"$tempCnpj", "Fornecedor"=>$tempFornecedor, "Data"=>"$tempData", "Nota"=>"$tempNota" ];
}
echo json_encode($arrResposta);
And no .js i have use
obj = JSON.parse(msg);

How to use PHP json_encode without UTF8?

I am working on an old existing website. All pages were encoded in ISO-European, including the MySQL database.
I want to add some AJAX using PHP's json_encode, which only supports UTF8.
Is there a solution to use json_encode without UTF8?
The only thing you need to do is to convert your data to UTF-8 before passing it to json_encode. That function requires UTF-8 data, and unless you want to reimplement json_encode yourself it's a lot easier to go along with its requirements:
function recursivelyConvertToUTF8($data, $from = 'ISO-8859-1') {
if (!is_array($data)) {
return iconv($from, 'UTF-8', $data);
}
return array_map(function ($value) use ($from) {
return recursivelyConvertToUTF8($value, $from);
}, $data);
}
echo json_encode(recursivelyConvertToUTF8($myData));
This is not necessarily a complete solution covering every possible use case, but it should illustrate the idea.
You can use var_export, utf8_encode and eval to convert an array to UTF-8 recursively. It's a bit of a hack, but something like the following works:
$obj = array("key" => "\xC4rger"); // "Ärger" in Latin1
eval('$utf8_obj = ' . utf8_encode(var_export($obj, TRUE)) . ';');
print json_encode($utf8_obj);
This will print
{"key":"\u00c4rger"}

MySQL JSON Encoded string corrupts after INSERT..SELECT

I'm encoding array of Image URLS into json string and store them in database. (utf8_general_ci).
When I insert data into table and retrive it, json_decode() is capable of decoding it.
However, when I copy data from one table to another (INSERT INTO ... SELECT statement) data after retrieving from database cannot be decoded anymore.
Instead, i get corrupted json ENCoded string. Even empty array [] cannot be properly decoded.
It converts from http://pl.tinypic.com/r/fwoiol/8
into http://pl.tinypic.com/r/bgea05/8
(had to make images since those squares cannot be copied as text).
Edit, After checking a bit more i tried to bin2hex() both strings from database.
Both seem to be exactly same.
However, one decodes and one does not. The
5b22687474703a5c2f5c2f7777772e
changes into
0022687474703a5c2f5c2f7777772e
So, json_decode only changes 5b into 00 in string.
It's like It's losing encoding somewhere?
Edit 2
static public function jsonDecodeFieldsArray($entries, $fields = array('features','images')){
foreach($entries as $key => $entry){
$entries[$key] = self::jsonDecodeFields($entry, $fields);
}
return $entries;
}
static public function jsonDecodeFields($entry, $fields = array('features','images')){
foreach($fields as $field){
if(isset($entry[$field])){
$entry[$field] = json_decode((string) $entry[$field], true);
}
}
return $entry;
}
I'm using code above, to decode keys of array specified by $fields. However, it not only decodes wrongfully. But also affects keys that are not listed in $fields. Corrupting their encodings.
More to add. If I dont use those functions and use only json_decode on fields json_decode($array[0][images], true) it works fine.
To Clarify that I found answer/solution I write this Answer
The reason behoind this error was not SQL error and data was proper. I had an example array of:
$many_entries = array(
array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
),
array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
)
);
// And
$one_entry = array(
'features' = > 'json_encoded_string'
'images' = > 'json_encoded_string'
);
Now I had 2 functions. One to Parse $many_entries (jsonDecodeFieldsArray) array and one to Parse $one_entry array structure (jsonDecodeFields).
The problem was I used jsonDecodeFieldsArray on $one_entry which made jsonDecodeFields iterate on strings.
It is odd that the character encoding is changing through the transmission. I would say check your charset(s) in PHP but you said the issue is only occurring in a table => table SQL transfer. I would still check the charset of the column / table.
You can fix the issue by running a str_replace() upon decoding. For example:
$DB_ARRAY = $DB_QUERY->fetch_array();
$CORRECT_ENCODING = json_decode(str_replace('0x93', '[', $DB_ARRAY['urlstring']), true);
You would, of course, need to know what the wrongly encoded character is. Or its ASCII code equivalent.

PHP. JSON encode utf-8

I want to encode json, but when I use json_encode function I get not UTF-8 string. I added header header('Content-Type: application/json; charset=utf-8'); and data from database comes good. How I could solve the problem?
My code:
foreach($dbh->query('SELECT Event.name, Event.description, Category.name as category FROM Event, Category WHERE Event.category_id = Category.category_id') as $row) {
$event['name'] = utf8_encode($row['name']);
$event['description'] = utf8_encode($row['description']);
$event['category'] = utf8_encode($row['category']);
$events[] = $event;
}
echo json_encode($events);
PHP json_encode needs always UTF8 string despite your charset. You must encode all your strings before.
To clarify, you must use utf8_encode on data extracted from your database if they are not already in utf8.
json_encode(array(
"one" => utf8_encode("super string &éùà"),
"two" => utf8_encode("super string &éùà")
));
Note : utf8_encode is only applicable from ISO 8859-1. If you are using another charset, see iconv()

Problem in converting string to puny code (in PHP, using phlyLabs's punycode string converter)

i'm using the code from here: http://phlymail.com/en/downloads/idna/download/ and built a function like this (from the example):
function convert_to_punycode($inputstring)
{
$IDN = new idna_convert();
// The input string, if input is not UTF-8 or UCS-4, it must be converted before
$inputstringutf8 = utf8_encode($inputstring);
// Encode it to its punycode presentation
$outputstringpunycode = $IDN->encode($inputstringutf8);
return $outputstringpunycode;
}
However it doesnt work properly.
For the input: Россию
It gives: РоÑÑиÑ
Whereas it should give: xn--h1alffa3f
What am I doing wrong? $inputstring which is being passed is a normal string with no special declarations/etc...
Is your string already UTF-8? Looks like it.
Or is it in ISO-8859-5?
In both cases you cannot use the PHP function utf8_encode(), since it expects your input string to be ISO-88591-1 (ISO Latin-1, Western European languages). Look into the file transcode_wrapper.php, which is delivered with the class source. This might help you.
you might need PHP IDNA Extension
I'd just add something like to use if possible the module otherwise Dave suggested function :
if(!function_exists('idn_to_ascii') and !function_exists('idn_to_utf8'))
{ define('IDN_FALLBACK_VERSION',2008);
require_once('idna_convert.class.php');
function idn_to_ascii($string)
{ $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
return $IDN->encode($string);
}
function idn_to_utf8($string)
{ $IDN = new idna_convert(array('idn_version'=>IDN_FALLBACK_VERSION));
return $IDN->decode($string);
}
function idn_to_unicode($string){return idn_to_utf8($string);}
}
Try this method to convert encoding
//$inputstringutf8 = utf8_encode($inputstring);
$inputstringutf8 = mb_convert_encoding($inputstring, 'utf-8', mb_detect_encoding($inputstring));

Categories