Valentino Ru Valentino Ru - 2 months ago 10
JSON Question

problems with german umlauts in php json_encode

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?

Answer

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 );
?>
Comments