简体   繁体   中英

How To Read A File in php wrappers as utf-16

Hello there is there a way to read a file in a specific format like utf-16 or any format that is not in the following: az, AZ, 0-9 Any help will be appreciated.

PHP strings don't know anything about encodings, so PHP file functions essentially treat every file as a binary file.

If you know that a set of bytes should be read as UTF-16, you can convert it to some other encoding of your choice (here using UTF-8 as an example) using any of these (depending which extensions you have installed):

// Requires ext/iconv; arguments are From, To, String
$utf8_string = iconv('UTF-16', 'UTF-8', $utf16_string);
// Requires ext/mbstring; arguments are String, To, From
$utf8_string = mb_convert_encoding($utf16_string, 'UTF-8', 'UTF-16');
// Requires ext/intl; arguments are String, To, From
$utf8_string = UConverter::transcode($utf16_string, 'UTF-8', 'UTF-16');

Conversely, if you know that the string is in some particular encoding (again, using UTF-8 as an example), and want it to be UTF-16, you would put things in the opposite order:

// Requires ext/iconv; arguments are From, To, String
$utf16_string = iconv('UTF-8', 'UTF-16', $utf8_string);
// Requires ext/mbstring; arguments are String, To, From
$utf16_string = mb_convert_encoding($utf8_string, 'UTF-16', 'UTF-8');
// Requires ext/intl; arguments are String, To, From
$utf16_string = UConverter::transcode($utf8_string, 'UTF-16', 'UTF-8');

In both cases, the resulting string is just a different sequence of bytes; other PHP functions still won't "know" what it "means".

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