使用php读取波斯(Unicode字符)文本文件 [英] Read Persian (Unicode chars) text file using php

查看:184
本文介绍了使用php读取波斯(Unicode字符)文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在借助以下代码读取一个波斯文本文件(使用PHP):

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文件内容:

name.txt file contents :

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

名称文件: (学习网络)

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

推荐答案

看到这个的原因是因为您只是在回显原始内容.您的浏览器将需要更多信息,以便以正确的形式显示消息.

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/>';
}

请注意,header函数需要在输出到浏览器的开始处执行.因此,例如,如果在此功能之前显示了其他数据,则需要将标题语句移到顶部.否则,您将在屏幕上收到警告,提示标题已设置.

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.

这篇关于使用php读取波斯(Unicode字符)文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆