I'm trying to convert an array with values in brazilian portuguese to JSON.
Here is an array example:
array(1) {
["title"]=>
string(77) "Cartão Credicard Universitário Visa Crédito "
}
If I use mb_detect_encoding
it shows that all values and keys are either in ASCII or UTF8.
However if I try to use json_encode
in order to generate the json, it returns a false and json_last_error
function says that the error is JSON_ERROR_UTF8
But if I apply first the utf8_encode_deep
function to the array ( http://php.net/manual/es/function.utf8-encode.php ), the json is generated without giving any errors.
The problem with this solution is that it returns certain words with bad codification.
Example:
Word before applying utf8_encode
: Cartão (good codification)
Word after applying utf8_encode
: Cartão (bad codification)
So although it generates the JSON, it doesn't solve my problem because it messes up the words.
Here is the code I'm using:
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$database;", $username, $password);
$sql = "SELECT title FROM card";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$json = $json_encode($result);
$error = json_last_error();
var_dump($json, $error === JSON_ERROR_UTF8);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage() . '\n';
}
If I try to connect to the database using charset=utf8 or charset=utf8mb4, it retrieves Cartão
(bad codification), instead of Cartão
(good codification)
I have also tried to use JSON_UNESCAPED_UNICODE
as parameter of json_encode
, but the result remains the same as without using this parameter.
Any suggestions?
UPDATE: I've simplified the example with one concrete case where this problem is happening.
UPDATE 2: Added some code in order to clarify the example, also added some explanations about possible solutions in the comments.