开发者

Read Chinese(Unicode chars) text file using PHP

I am reading one Chinese text file (used PHP) with the help of below code :

    $myFile = "t开发者_JAVA技巧est.txt";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh); 
    print $theData;

test.txt file contents :

PHP是很好的语言。我喜欢这门语言。
我正在开发一个Web应用程序使用PHP。

After executing above mentioned code, I was not getting exact content which is in above mentioned file.

Is there any other way to read such file which contains Unicode characters and print it.

Please provide me your suggestions.

Thanks,

-Pravin


You need to specify an output encoding to match the encoding of the character data you're reading.

If your text file is UTF-8 encoded, add one of the following content-type headers to the top of your PHP file:

header("Content-Type: text/plain; charset=UTF-8");  // output as text file
header("Content-Type: text/html; charset=UTF-8");   // output as HTML 

if it is UTF-16 encoded, I would use iconv() to convert it to UTF-8, which has become pretty much the web's standard encoding:

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


Better way is use this:

readfile('text.txt');

Documentation for this function you can find at http://pl2.php.net/manual/en/function.readfile.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜