简体   繁体   中英

json_encoded array to utf8_encoded string, write to file, file should be utf8 - php

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM