Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP读取中文(Unicode字符)文本文件_Php_File Io - Fatal编程技术网

使用PHP读取中文(Unicode字符)文本文件

使用PHP读取中文(Unicode字符)文本文件,php,file-io,Php,File Io,我正在阅读一个中文文本文件(使用PHP),其代码如下: $myFile = "test.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); print $theData; test.txt文件内容: PHP是很好的语言。我喜欢这门语言。 我正在开发一个Web应用程序使用PHP。 在执行上述代码之后,我并没有得到上述文件中的确切内

我正在阅读一个中文文本文件(使用PHP),其代码如下:

    $myFile = "test.txt";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh); 
    print $theData;
test.txt文件内容:

PHP是很好的语言。我喜欢这门语言。
我正在开发一个Web应用程序使用PHP。
在执行上述代码之后,我并没有得到上述文件中的确切内容

有没有其他方法可以读取包含Unicode字符的文件并打印出来

请给我你的建议

谢谢


-Pravin更好的方法是使用:

readfile('text.txt');

此函数的文档可以在

中找到。您需要指定输出编码,以匹配正在读取的字符数据的编码

如果文本文件是UTF-8编码的,请在PHP文件的顶部添加以下
内容类型
标题之一:

header("Content-Type: text/plain; charset=UTF-8");  // output as text file
header("Content-Type: text/html; charset=UTF-8");   // output as HTML 
如果它是UTF-16编码的,我会使用
iconv()
将其转换为UTF-8,这几乎已经成为web的标准编码:

$theData = iconv("UTF-16", "UTF-8//TRANSLIT", $theData);

谢谢,我使用了$theData=iconv(“UTF-16”、“UTF-8//translatit”、$theData);这适用于中文,但不适用于普通英文,阅读英文文本文件内容后,与原始英文文本不匹配