如何在PHP中将UCS-2文本文件读作UTF-8字符串?

时间:2015-09-04 12:57:25

标签: php string utf-8 ucs2

我有一个UCS-2文本文件。现在,我想将此文本文件作为UTF-8字符串读取。我已经使用这段代码来完成它。

my_code.php

<?php

error_reporting(0);        
header('Content-Type: text/html; charset=utf-8');

echo '<form enctype="multipart/form-data" method="post"><p><input type="file" name="my_file" />&nbsp;<input type="submit" value="+" /><hr />';

$my_str = file_get_contents(($_FILES['my_file']['tmp_name']));
echo $my_str;

?>

viet_test.txt

"Vietnamese" is "Tiếng Việt".

但是,它返回错误��"Vietnamese" is "Ti�ng Vi�t".。我正在寻找:"Vietnamese" is "Tiếng Việt"(UTF-8)。

你能告诉我:“我的代码有什么问题?以及如何修复它?”。

对不起,我在PHP方面不是很专业。

1 个答案:

答案 0 :(得分:1)

您无法读取文件&#34;为UTF-8&#34;。它包含UCS-2,因此读取它您将读取UCS-2字符串。但是,您可以读取的UCS-2字符串转换为UTF-8:

$my_str = file_get_contents($_FILES['my_file']['tmp_name']);
$my_str = mb_convert_encoding($my_str, 'UTF-8', 'UCS-2');
echo $my_str;

请注意,您可能必须明确使用UCS-2BEUCS-2LE 如果仍然没有&#34;什么都没有&#34;,那么你有一个与编码无关的问题。