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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?