Problems with German Umlauts in PHP JSON_Encode

problems with german umlauts in php json_encode

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

Problems with german umlauts in PHP

The problem is not in MySQL. Actually, the fact that you get that unicode notation proves that the data is in fact interpreted as a UTF-8 character (and probably the right character as well ;)).

The problem is in encoding it to json. By default json_encode escapes those characters.

That is not wrong by the way. You are allowed to escape them, but JSON also allows UTF-8 characters to be unescaped. JSON isn't meant to be human readable per se. It's just a format to send data over the line. The receiving party should be able to unescape the unicode sequence and show it as a ü.

Anyway, since JSON can also contain unescaped characters, you don't need to escape them, and you can tell json_encode not to by passing a flag:

json_encode($myArray, JSON_UNESCAPED_UNICODE);

See json_encode function and json constants for more details.

PHP json_encode umlauts

That is the default behaviour of the json_encode function, but you can override this by specifying the JSON_UNESCAPED_UNICODE option. So for example:

json_encode("Außenministerium", JSON_UNESCAPED_UNICODE);

And in your code you should do:

fwrite($fp, json_encode($post, JSON_UNESCAPED_UNICODE));

Also check out which other options you can use in the documentation

Can't show json object in case of german umlauts

I solved issue like this, on my dbConnect php file i added mysqli_set_charset, and my dbConnect.php looks like this. I hope this helps someone.

$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'local_db';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
mysqli_set_charset($mysqli, 'utf8');

Json_encode umlauts output

In case you are using PHP 5.4+, change echo json_encode($json_full); to echo json_encode($json_full, JSON_UNESCAPED_UNICODE);

Full list of json_encode predefined constants can be found there: http://lt1.php.net/manual/en/json.constants.php

Demonstration

Code:

<?php
$json = (object) array(
"uml" => "fü",
);

echo json_encode($json, JSON_UNESCAPED_UNICODE) . PHP_EOL;

echo json_encode($json) . PHP_EOL;

Output:

{"uml":"fü"}
{"uml":"f\u00fc"}

German Umlaute not considered in JSON_CONTAINS() with MariaDB

Thank you all for your inputs and response!

I figured out a different solution for the problem. Maybe it helps someone..

I went a step back and checked how I am storing the data. I was using json_encode() for that, which created the table contents as shown above. By just using a raw array to save it, it was working then

$insert->pilotname = ['pilotname' => $request->pilotname];

Somehow the storing of data before was already the issue.

German Umlaut / Special characters in userInfo NSDictionary

Have a look at this article on stackoverflow. Both of the solution i post are there with some comments. Hop it helps you.

Solution 1:

You can define the document type with utf-8 charset (like written in a comment on qour question):

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

Solution 2:

Another solution would be to use htmlentities().

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

echo json_encode( $test );
?>


Related Topics



Leave a reply



Submit