17

I'm getting troubles with data from a database containing german umlauts. Basically, whenever I receive a data containing umlauts, it is a black square with an interrogation mark. I solved this by putting

mysql_query ('SET NAMES utf8')

before the query.

The problem is, as soon as I use json_encode(...) on a result of a query, the value containing an umlaut gets null. I can see this by calling the php-file directly in the browser. Are there other solution than replacing this characters before encoding to JSON and decoding it in JS?

5 Answers 5

41

Check out this pretty elegant solution mentioned here:

json_encode( $json_full, JSON_UNESCAPED_UNICODE );

If the problem isn't anywhere else in your code this should fix it.

Edit: Umlaut problems can be caused by a variety of sources like the charset of your HTML document, the database format or some previous php functions your strings run through (You should definitely look into multibyte functions when having problems with umlauts).

These problems tend to be the pretty annoying because they are hard to track in most cases (altough this isn't as bad as it was a few years ago). The function above fixes – as asked – umlaut problems with json_encode … but there is a good chance that the problem is caused by a different part of your application and not this specific function.

2
  • 1
    This should be the correct answer. saved a lot of time, thanks
    – atx
    Nov 28, 2018 at 21:22
  • While the accepted answer had many problems, this answer worked for me.
    – mdikici
    Feb 1, 2021 at 23:42
15

I know this might be old but here a better solution:

Define the document type with utf-8 charset:

<?php header('Content-Type: application/json; charset=utf-8'); ?>

Make sure that all content is utf_encoded. JSON works only with utf-8!

function encode_items(&$item, $key)
{
    $item = utf8_encode($item);
}
array_walk_recursive($rows, 'encode_items');

Hope this helps someone.

6

You probably just want to show the texts somehow in the browser, so one option would be to change the umlauts to HTML entities by using htmlentities().

The following test worked for me:

<?php
    $test = array( 'bla' => 'äöü' );
    $test['bla'] = htmlentities( $test['bla'] );

    echo json_encode( $test );
?>
0
5

The only important point here is that json_encode() only supports UTF-8 encoding. http://www.php.net/manual/en/function.json-encode.php

All string data must be UTF-8 encoded.

So when you have any special characters in a non utf-8 string json_encode will return a null Value.

So either you switch the whole project to utf-8 or you make sure you utf8_encode() any string before using json_encode().

0

make sure the translation file itself was explicitely stored as UTF-8

After that reload cache blocks and translations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.