将UTF-16LE转换为UTF-8 [英] Convert UTF-16LE to UTF-8 in php

查看:1411
本文介绍了将UTF-16LE转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 iconv php函数,但某些字符无法正确转换:

  ... 
$ s = iconv('UTF-16','UTF-8',$ s);
...
$ s = iconv('UTF-16 // IGNORE','UTF-8',$ s);
...
$ s = iconv('UTF-16LE','UTF-8',$ s);
...
$ s = iconv('UTF-16LE // IGNORE','UTF-8',$ s);
...

我也尝试 mb_convert_encoding 函数,但不能解决我的问题。



一个示例文本文件:9px.ir/utf8-16LE.rar

解决方案

iconv 支持 UTF-16LE 编码



您可以使用它将编码从 UTF-16LE 转换为 UTF-8



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
if(false === $ result)
{
throw new Exception('Input string could not be conversion。');
}

请参阅



我只是想知道如果所有代码点都可以在 UTF-16LE 可在 UTF-8 。但是我认为这应该适合你的情况。






编辑:在我自己的盒子上重现问题,但是在另一个框中,我遇到了这个通知:


注意:iconv()[function.iconv]:错误的字符集,从 UTF-16LE' / code> UTF-8'不允许...


看起来不是全部 iconv 可以将 UTF-16LE 转换为 UTF-8



它可能是使用 mb_convert_encoding 文档 ,至少在这种情况下(演示):

  $ result = mb_convert_encoding($ str,'UTF-8','UTF-16LE'); 


I use iconv php function but some characters doesn't convert correctly:

...
$s = iconv('UTF-16', 'UTF-8', $s);
...
$s = iconv('UTF-16//IGNORE', 'UTF-8', $s);
...
$s = iconv('UTF-16LE', 'UTF-8', $s);
...
$s = iconv('UTF-16LE//IGNORE', 'UTF-8', $s);
...

I also try mb_convert_encoding function but can't solve my problem.

A sample text file: 9px.ir/utf8-16LE.rar

解决方案

iconv supports the UTF-16LE encoding.

You can use it to transpose the encoding from UTF-16LE to UTF-8:

$result = iconv($in_charset = 'UTF-16LE' , $out_charset = 'UTF-8' , $str);
if (false === $result)
{
    throw new Exception('Input string could not be converted.');
}

See iconvDocs.

I'm just wondering if all code-points available in UTF-16LE are available in UTF-8. But I assume that this should fit in your case.


Edit: I was not able to reproduce the problem on a box of my own, but on another box I ran into this notice:

Notice: iconv() [function.iconv]: Wrong charset, conversion from UTF-16LE' toUTF-8' is not allowed in ...

Looks like that not all iconv versions can actually convert UTF-16LE to UTF-8.

It might be a workaround to use mb_convert_encodingDocs instead, at least it was in this case (Demo):

$result = mb_convert_encoding($str , 'UTF-8' , 'UTF-16LE');

这篇关于将UTF-16LE转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆