0

here my code:

$array_test = array();

$array_test[0] = "test1";

$array_test[1] = "test2";

$array_test = json_encode($array_test);

$array_test = utf8_encode($array_test);

$myfile = fopen($Pfad, "w+");

file_put_contents($Pfad, $neueZeile, FILE_APPEND);

fclose($myfile)

Problem here, when I write it as this I will get an ANSI encoded file.

As I understand the file will be UTF8 encoded if I put UTF8 encoded content into it.

If I utf8_encode a simple string it will work, but not the whole json_encoded array.

No I´m stucked, cause niether way it works. If I just utf8_encode the array content by itself and then json_encode it, the file will be still set as ANSI.

Anybody some idea, is there a way, do I understand something wrong?

I try to set the whole process of loading reading and saving to the file as utf8, but this makes it not possible for me right now.

Thank you for reading this, Josef

1 Answers1

0

Try to use Byte Order Mark. This can be done in this way

<?php
$array = array("test1", "test2");

$json = json_encode($array);
$utf8_string = utf8_encode($json);
$utf8_w_bom = "\xEF\xBB\xBF" . $utf8_string; // Add BOM to the start of the string

file_put_contents($Pfad, $utf8_w_bom);
Diblo Dk
  • 390
  • 6
  • 20