在 php 中将 UTF-16LE 转换为 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6980068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:44:15  来源:igfitidea点击:

Convert UTF-16LE to UTF-8 in php

phpencodingiconv

提问by ??????

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

我使用iconvphp 函数,但有些字符不能正确转换:

...
$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_encodingfunction but can't solve my problem.

我也尝试mb_convert_encoding函数但无法解决我的问题。

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

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

回答by hakre

iconvsupports the UTF-16LEencoding.

iconv支持UTF-16LE编码

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

您可以使用它来将编码从UTF-16LEto转置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.

请参阅iconv文档

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

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



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

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

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

注意: iconv() [function.iconv]: 字符集错误,UTF-16LE' to在 ... 中不允许从UTF-8'转换

Looks like that not all iconvversions can actually convert UTF-16LEto UTF-8.

看起来并非所有iconv版本都可以实际转换UTF-16LEUTF-8.

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

使用mb_convert_encodingDocs可能是一种解决方法,至少在这种情况下是这样(演示):

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