martinszeltins's avatar

How to display UTF-16 file?

I have a simple .csv file but it is UTF-16 encoded, so the output I get is very strange.

My file contents:

"Phone number"
"+1 5556669999"

This is the output I get

dd( file_get_contents($file->getPathName()) );

"""
"\x00P\x00h\x00o\x00n\x00e\x00 \x00n\x00u\x00m\x00b\x00e\x00r\x00"\x00\n
\x00"\x00+\x001\x00 \x005\x005\x005\x000\x000\x007\x001\x008\x00"\x00\n
\x00"\x00+\x001\x00 \x005\x005\x005\x005\x001\x009\x001\x009\x00"\x00\n
\x00
"""

How could I get the correct output instead of this gibberish?

Also I would like to turn it into an associative array, how could I do this?

0 likes
1 reply
s4muel's avatar

try running it through Rasmus Andersson`s function:

function utf16_decode($str)
{
    if (strlen($str) < 2) return $str;
    $bom_be = true;
    $c0 = ord($str{0});
    $c1 = ord($str{1});
    if ($c0 == 0xfe && $c1 == 0xff) {
        $str = substr($str, 2);
    } elseif ($c0 == 0xff && $c1 == 0xfe) {
        $str = substr($str, 2);
        $bom_be = false;
    }
    $len = strlen($str);
    $newstr = '';
    for ($i = 0; $i < $len; $i += 2) {
        if ($bom_be) {
            $val = ord($str{$i}) << 4;
            $val += ord($str{$i + 1});
        } else {
            $val = ord($str{$i + 1}) << 4;
            $val += ord($str{$i});
        }
        $newstr .= ($val == 0x228) ? "\n" : chr($val);
    }
    return $newstr;
}

source: http://php.net/manual/en/function.utf8-decode.php#49185

no guarantee though;)

Please or to participate in this conversation.