开发者

PHP: file() each character is preceded by "\0"

开发者 https://www.devze.com 2023-03-19 05:58 出处:网络
I have an INI file that has been created by an AutoHotKey program written by another member of a team I am working on. I am trying to read the file into PHP as an array. I am using the built in file()

I have an INI file that has been created by an AutoHotKey program written by another member of a team I am working on. I am trying to read the file into PHP as an array. I am using the built in file() function. When I read in the file and then display it using var_dump($file) ($file is the name of the array the file is being read into) each character is preceded by a "\0"(which is a null character). Each Has an开发者_如何转开发yone seen this happen before? I could just remove all the null characters after I read in the file, but I would prefer to find a solution.

When I open the ini file in text editors it opens just fine. I also read in using the file() function the desktop.ini file created by Windows and it read in as expected, the characters were not preceded by "\0"


It has to do with the encoding of the file. Some (most) unicode encodings require 2 bytes per character. This means that each single byte character is prefixed with a \0. The file is still perfectly valid, but if you try to read it with a windows-1252 type single byte encoding it will look odd. You can try feeding it through utf8_decode and see if that clears it up. UTF 8 is a potential multibyte encoding from 1 to 4 bytes (with a minimum of 1). Your text editor automatically figure this out and opens it as unicode which is why it looks fine there.

Note: because it uses a 2 byte encoding, it is specifically not utf-8, it may be utf-16, but the UTF-8 functions may be able to parse it.


have you opened the file in a HEX Editor? There are probably hidden characters in your file...

0

精彩评论

暂无评论...
验证码 换一张
取 消