Convert UCS-2 file to UTF-8 with PHP

2019-02-15 01:12发布

I have a CSV file supplied from a client which has to be parsed and inserted into a database using PHP.

Before inserting the data into the DB, I want to convert it to UTF-8 but I cant seem to find how.

This is what I got trying to detect the files encoding:

$ enca -d -L zh ./artigos.txt 
    ./artigos.txt: Universal character set 2 bytes; UCS-2; BMP
    CRLF line terminators
    Byte order reversed in pairs (1,2 -> 2,1)

I tried using the iconv function but it messes up the conversion and shows the result with diferent characters than the originals.

First line of the file (base64 encoded):

IgAwADMAMQAxADkAIgAsACIANwAzADEAMwA0ADYAMgA2ADQAMAAwADEANQAiACwAIgBBAGcAcgBhAGYAYQBkAG8AcgAgAFIAYQBwAGkAZAAgADkAIABIAGUAYQB2AHkAIABEAHUAdAB5ACIALAAiAEEAZwByAGEAZgBvACAAOQAvADgALAAgADkALwAxADAALAAgADkALwAxADIALAAgADkALwAxADQAIgAsACIAMQAxADAAZgBsAHMAIgAsACIAIgAsACIAIgAsACIAIgAsACIAMAAzADEAMQA5AC4AagBwAGcAIgAsACIAIgAsACIAMQAsADIAMAAiACwAIgA1ADkALAA5ADAAIgAsACIAMgAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIARgBhAGwAcwBlACIADQAK

标签: php encoding
3条回答
Bombasti
2楼-- · 2019-02-15 01:19

This seems to work(little endian), althoug you didnt include any non ascii chars

$s='IgAwADMAMQAxADkAIgAsACIANwAzADEAMwA0ADYAMgA2ADQAMAAwADEANQAiACwAIgBBAGcAcgBhAGYAYQBkAG8AcgAgAFIAYQBwAGkAZAAgADkAIABIAGUAYQB2AHkAIABEAHUAdAB5ACIALAAiAEEAZwByAGEAZgBvACAAOQAvADgALAAgADkALwAxADAALAAgADkALwAxADIALAAgADkALwAxADQAIgAsACIAMQAxADAAZgBsAHMAIgAsACIAIgAsACIAIgAsACIAIgAsACIAMAAzADEAMQA5AC4AagBwAGcAIgAsACIAIgAsACIAMQAsADIAMAAiACwAIgA1ADkALAA5ADAAIgAsACIAMgAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIAMAAiACwAIgAwACIALAAiADAAIgAsACIARgBhAGwAcwBlACIADQAK';
$t=base64_decode($s);
echo iconv('UCS-2LE', 'UTF-8', substr($t, 0, -1));//last byte was invalid
查看更多
Deceive 欺骗
3楼-- · 2019-02-15 01:24

Microsoft Excel CSV are generally Little Endian encoded (took me long to find out). If you want to use them with e.g. fgetcsv you should convert the file into UTF-8 before. I do the following:

        $str=file_get_contents($file);
        $str= mb_convert_encoding($str, 'UTF-8', 'UCS-2LE'); 
        file_put_contents("converted_".$file, $str);
查看更多
The star\"
4楼-- · 2019-02-15 01:32

python :

One of the method to encode is

Text -> utf-16-be -> hexadecimal

Convert back

hexadecimal to binary and then from utf-16-be to text

Note : ucs-2be is deprecated and move to utf-16-be

Decoder

import binascii
code = '098 ... '
decoded_text = binascii.unhexlify(code).decode('utf-16-be')
查看更多
登录 后发表回答