I'm getting json strings from an HTTP API. The content type is Content-Type: application/json; charset=UTF-8.
I then proceed to write the strings to a file using.
fwrite($fp, json_encode($post));
The strings contain encodings for umlauts as follows.
Au\u00dfenministerium verh\u00e4ngte Reisewarnung f\u00fcr Kroatien in Kraft getreten.
This should be.
Außenministerium verhängte Reisewarnung für Kroatien in Kraft getreten.
How can I encode the strings to write umlauts to files and not their encoding?
I tried the following.
<?php
$string = "Au\u00dfenministerium verh\u00e4ngte Reisewarnung f\u00fcr Kroatien in Kraft getreten.";
$string = utf8_encode($string);
echo $string;
The output of this script still shows the encoding.
<meta charset="utf-8">on the page?utf8_encode(), it's a terribly-named function that doesn't do what you think it does. It's a forced conversion that doesn't detect or care about the input encoding and will corrupt your data more often than not. Same goes forutf8_decode().