I ask to help PHP experts. Trying to read json file:

{ "logo": { "position":"right", "width":"180" }, "image": { "size":"big" }, "menu": { "items":"Русский" } } 

PHP code:

 header('Content-type: text/html; charset=UTF-8'); $file = file_get_contents('tegs.json', FILE_USE_INCLUDE_PATH); $obj = json_decode($file, true); print $obj["logo"]; 

When I delete Russian characters from a file, everything is read and works. But with the Cyrillic alphabet is not friendly. The php file encoding is utf-8. I tried different encodings of json-file: ANSI, Unicode, Utf-8.

  • show the contents in file_get_contents - Invision

1 answer 1

First, check the coding of your content with the following construction

 $is_valid_utf8 = mb_check_encoding($file, 'utf-8')); 

mb_check_encoding Returns TRUE if successful, or FALSE if an error occurs.

Check that the json file is required in UTF-8 without BOM! If it is not possible to transcode it to UTF-8 without BOM (for example, Notepad ++: encoding menu - convert to UTF-8 without BOM), then you can cut it out like this before json_encode:

 if(substr($file, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) { $file = substr($file, 3); }