Quick solution.
Just copy and paste.
You do not need to use your brain too much.
while ($r = mysql_fetch_assoc($result)){
$linhas[] = $r;
}
echo json_encode($linhas);
Switch to this and everything is yours!
while ($r = mysql_fetch_assoc($result)){
$linhas[] = utf8_decode($r);
}
echo json_encode($linhas, JSON_UNESCAPED_UNICODE);
The rest of the answer is in case you want to understand what was done.
Continue reading if you want to fix the problem correctly.
Detailed answer.
note: It needs a little brain to continue reading. Texts with more than 3 lines give sleep. Good luck.
Concerning the question
I do not quite understand what you really want, but I do not understand what you want to display in json's return, the characters in their original form.
The json_encode () function automatically encodes special characters and applies escape characters. So you get the result as uxxxx
.
Simple solution (PHP5.4 +)
A simple way to solve is to set the second parameter of the json_encode () function. There is a constant called JSON_UNESCAPED_UNICODE , which can be used like this:
echo json_encode(array('acentuação'), JSON_UNESCAPED_UNICODE);
// retorna
// ["acentuação"]
echo json_encode(array('acentuação'));
// retorna
// ["acentua\u00e7\u00e3o"]
Retrocompatibility
To ensure greater compatibility, here's an example of how to create a backward compatibility with PHP versions below 5.4:
The reason for this is that this feature of the json_encode()
function is available from PHP5.4. Currently it is still common to find servers with PHP lower than version 5.4, so it is still valid to apply this technique.
function JsonEncode($val, $option = null)
{
if (empty($option)) {
return json_encode($val);
}
if (PHP_VERSION >= 5.4) {
return json_encode($val, JSON_UNESCAPED_UNICODE);
} else {
// $option == JSON_UNESCAPED_UNICODE
$encoded = json_encode($val);
//$unescaped = preg_replace_callback('/\\u(\w{4})/', function ($matches) {
$unescaped = preg_replace_callback(
'/(?<!\\)\\u(\w{4})/',
function ($matches) {
return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
},
$encoded
);
return $unescaped;
}
}
// Usage sample
echo JsonEncode(array('acentuação'));
// retorna
// ["acentuação"]
Double condition
According to @Bacco, your original code is probably double-coding, so it generates an erroneous code.
The unicode format of string ção
is \u00e7\u00e3o
. However, in the code presented in the question it displays \u00c3\u00a7\u00c3\u00a3o
In a simple test, I simulated double coding and hit 100% with the result of the question:
$str = 'acentuação';
$str_utf8 = utf8_encode($str);
echo PHP_EOL.$str_utf8;
echo PHP_EOL.json_encode($str_utf8);
// retorna
//u00c3/u00a7/u00c3/u00a3o_810
// Esse aqui é o correto como deveria retornar
echo PHP_EOL.json_encode(array($str));
To solve your specific case, there is an easy and dumb option that is to sweep the dirt under the rug:
utf8_decode(json_encode($val, JSON_UNESCAPED_UNICODE));
The second option is to do the "right thing" by eliminating the root problem. Find out where you are applying the double coding and correct. This eliminates unnecessary use of utf8_encode () and utf8_decode () / a>.
The complete test code: link
Note: The use of utf8_encode () and utf8_decode () is unnecessary under the context in question. It does not mean that it's totally unnecessary, but rather that it's being misused.