dtip91401 2013-10-25 15:15
浏览 192
已采纳

如何使用PHP将UTF-8数据写入UTF-16LE文件?

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>
";
$copy .= "<Version:8><FeatureSet:InDesign-Roman><ColorTable:=<Black:COLOR:CMYK:Process:0,0,0,1>>
";
$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);
  • 写回答

3条回答 默认 最新

  • duanchensou8685 2013-10-26 13:16
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题