duanche2007 2012-09-25 18:05
浏览 137
已采纳

使用php读取波斯语(Unicode字符)文本文件

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

/* Reading the file name and the book (UTF-8) */
if(file_exists($SourceDirectoryFile))
{
        $NameBook  = "name.txt";
        $AboutBook = "about.txt";

        $myFile = "Computer-Technolgy/2 ($i)/".$NameBook;
        $fh = fopen($myFile, 'r');
        $theData = fread($fh, filesize($myFile));
        fclose($fh);
        echo 'Name file: '. $theData.'<hr/>';
}

name.txt file contents :

آموزش شبكه هاي کامپيوتري (LEARNING NETWORK)

Name file: ����� ���� ��� ��������� (LEARNING NETWORK)

  • 写回答

3条回答 默认 最新

  • douou8954 2012-09-25 18:31
    关注

    The reason you are seeing this is because you are just echoing the contents raw. Your browser will need more information, in order to display the message in its correct form.

    The easiest way is to use the snippet below.

    /* Reading the file name and the book (UTF-8) */
    if (file_exists($SourceDirectoryFile))
    {
    
        $NameBook  = "name.txt";
        $AboutBook = "about.txt";
    
        // Using file_get_contents instead. Less code
        $myFile   = "Computer-Technolgy/2 ($i)/" . $NameBook;
        $contents = file_get_contents($myFile);
    
        // I want my browser to display UTF-8 characters
        header('Content-Type: text/html; charset=UTF-8');
        echo 'Name file: ' .  $contents . '<hr/>';
    }
    

    Please note that the header function needs to be executed at the beginning of the output to the browser. So for instance if you have additional data that is displayed prior to this function, you need to move the header statement at the top. Otherwise you will end up with warnings on screen that the headers have already been set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果