問題描述
我使用 iconv
php 函數,但有些字符不能正確轉換:
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);
...
我也嘗試了 mb_convert_encoding
函數,但無法解決我的問題.
I also try mb_convert_encoding
function but can't solve my problem.
示例文本文件:9px.ir/utf8-16LE.rar
A sample text file: 9px.ir/utf8-16LE.rar
推薦答案
iconv
支持UTF-16LE
編碼.
您可以使用它來將編碼從 UTF-16LE
轉置為 UTF-8
:
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.');
}
參見iconv
文檔.
我只是想知道 UTF 中的所有代碼點是否可用-16LE
在 UTF-8
中可用.但我認為這應該適合您的情況.
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.
我無法在自己的盒子上重現該問題,但在 另一個框我遇到了這個通知:
I was not able to reproduce the problem on a box of my own, but on another box I ran into this notice:
注意:iconv() [function.iconv]: 字符集錯誤,從 UTF-16LE' 到
UTF-8' 的轉換是不允許的 ...
Notice: iconv() [function.iconv]: Wrong charset, conversion from
UTF-16LE' to
UTF-8' is not allowed in ...
看起來并不是所有的 iconv
版本都能真正將 UTF-16LE
轉換為 UTF-8
.
Looks like that not all iconv
versions can actually convert UTF-16LE
to UTF-8
.
使用 mb_convert_encoding
Docs 相反,至少在這種情況下是這樣 (演示):
It might be a workaround to use mb_convert_encoding
Docs instead, at least it was in this case (Demo):
$result = mb_convert_encoding($str , 'UTF-8' , 'UTF-16LE');
這篇關于在 php 中將 UTF-16LE 轉換為 UTF-8的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!