I am reading from database with some text in Hebrew and trying to json_encode
it.
if i print_r
the results i get:
Array
(
[0] => Array
(
[value] => 88
[text] => כיתה א'
[parent_id] => 1
[level] => 1
)
[1] => Array
(
[value] => 89
[text] => כיתה ב'
[parent_id] => 1
[level] => 1
)
[2] => Array
(
[value] => 91
[text] => כיתה ג'
[parent_id] => 1
[level] => 1
)
)
while the json_encode shows:
[{"value":"88","text":null,"parent_id":"1","level":"1"},{"value":"89","text":null,"parent_id":"1","level":"1"},{"value":"91","text":null,"parent_id":"1","level":"1"}]
i belive it's because my text from the database contains a ( ' ) mark. tried various combination of stripslashes or real_escape_string none have helped.
json_encode
expects strings in the data to be encoded as UTF-8.
Convert them to UTF-8 if they aren't already:
$results = array_map(function($r) {
$r['text'] = utf8_encode($r['text']);
return $r;
}, $results);
echo json_encode($results);
Best and quickest solution I found was in the PHP Reference itself, by Sam Barnum
$encodedArray = array_map(utf8_encode, $rawArray);
$dbh = new PDO('mysql:host=localhost;dbname=test',
$user,
$pass
);
$dbh->exec("SET CHARACTER SET utf8");
or
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname",
$dbuser,
$dbpass,
array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With