0

Given a string of UTF-8 data in PHP, how can I convert and save it to a UTF-16LE file (this particular file happens to be destined for Indesign - to be placed as a tagged text document).

Data:

$copy = "<UNICODE-MAC>\n";
$copy .= "<Version:8><FeatureSet:InDesign-Roman><ColorTable:=<Black:COLOR:CMYK:Process:0,0,0,1>>\n";
$copy .= "A bunch of unicode special characters like ñ, é, etc.";

I am using the following code, but to no avail:

file_put_contents("output.txt", pack("S",0xfeff) . $copy);
murdaugh
  • 121
  • 1
  • 3

3 Answers3

3

You can use iconv:

$copy_utf16 = iconv("UTF-8", "UTF-16LE", $copy);
file_put_contents("output.txt", $copy_utf16);

Note that UTF-16LE does not include a Byte-Order-Marker, because the byte order is well defined. To produce a BOM use "UTF-16" instead.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • The file created by this (without the BOM) alone would not open in InDesign (it did not detect it as a UTF-16LE file), but I have found a solution by packing the UTF-16LE BOM, reversing the Byte Order _and_ with the icon you suggested. Thanks. – murdaugh Oct 26 '13 at 13:27
1

Using the following code, I have found a solution:

this function changes the byte order (from http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/):

function chbo($num) {
    $data = dechex($num);
    if (strlen($data) <= 2) {
        return $num;
    }
    $u = unpack("H*", strrev(pack("H*", $data)));
    $f = hexdec($u[1]);
    return $f;
}

used with a utf-8 to utf-16LE conversion, it creates a file that will work with indesign:

file_put_contents("output.txt", pack("S",0xfeff). chbo(iconv("UTF-8","UTF-16LE",$copy));
murdaugh
  • 121
  • 1
  • 3
0

Alternatively, you could use mb_convert_encoding() as follows:

$copy_UTF16LE = mb_convert_encoding($copy,'UTF-16LE','UTF-8');
r3mainer
  • 23,981
  • 3
  • 51
  • 88