PHP文件打开编码问题? [英] PHP File Opening Encoding Problem?

查看:156
本文介绍了PHP文件打开编码问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试用PHP打开一个游戏创建的.log文件时,我得到了一堆这个。

 ÿþ ************************* ************************* * * * * * * * * * *  
K 2 E n g i n e s t a r t u p . . .
[ 2 0 0 9 / 2 2 / 0 9 ]
[ 1 6 : 0 7 : 3 3 ]
[ 0 . 1 . 4 6 . 0 ]
[ 0 ]

我不知道为什么?我的代码是

  $ file = trim($ _ GET ['id']); 
$ handle = @fopen($ file,a +);

if($ handle){
print< table>;
while(!feof($ handle)){
$ buffer = stream_get_line($ handle,10000,\\\
);
echo< tr>< td width = 10> 。 __LINE__。 < / TD>< TD> 中。 $ buffer。 < / TD>< / TR> 中;
}
print< / table>;

fclose($ handle);

我正在使用 stream_get_line ,因为它是对于大文件显然更好吗?

解决方案

PHP对编码不太了解。特别是,它对文件的编码一无所知。



数据看起来像UTF-16LE。所以你需要把它转换成你可以处理的东西 - 或者,由于你只是打印,你可以转换整个脚本,输出它的HTML为UTF-16LE。



我可能更喜欢转换为UTF-8并将其用作页面编码,因此您确定没有字符丢失。假设可以使用 iconv (PHP扩展名为在Windows上需要,我相信)。注意,无论你做什么,你应该剥离第一行的前两个字符,假设编码总是相同的。在您显示的数据中,这些字符是字节顺序标记,它告诉我们文件的编码(如前所述,UTF-16LE)。



但是,看到它似乎是纯文本,所有你正在做的是打印数据,考虑只是打开它在一个普通的旧文本编辑器(支持Unicode)。不知道您的操作系统,我犹豫建议一个特定的操作系统,但是如果你在Windows上,文件比较小,记事本就可以这样做。



作为附注, __ LINE __ 不会给您您正在阅读的文件的行号,它将打印当前正在执行的脚本行的行号。


When I try to open a .log file created by a game in PHP I get a bunch of this.

ÿþ*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*�*� �
K�2� �E�n�g�i�n�e� �s�t�a�r�t� �u�p�.�.�.� �
[�2�0�0�9�/�2�2�/�0�9�]� �
[�1�6�:�0�7�:�3�3�]� �
[�0�.�1�.�4�6�.�0�]� �
[�0�]� �

I have no idea as to why? My code is

$file = trim($_GET['id']);
$handle = @fopen($file, "a+");

    if ($handle) {
        print "<table>";
        while (!feof($handle)) {
            $buffer = stream_get_line($handle, 10000, "\n");
            echo "<tr><td width=10>" . __LINE__ . "</td><td>" . $buffer . "</td></tr>";
        }
        print "</table>";

fclose($handle);

I'm using stream_get_line because it is apparently better for large files?

解决方案

PHP doesn't really know much about encodings. In particular, it knows nothing about the encoding of your file.

The data looks like UTF-16LE. so you'll need to convert that into something you can handle - or, since you're just printing, you can convert the entire script to output its HTML as UTF-16LE as well.

I would probably prefer converting to UTF-8 and using that as the page encoding, so you're sure no characters are lost. Take a look at iconv, assuming it's available (a PHP extension is required on Windows, I believe).

Note that regardless of what you do, you should strip the first two characters of the first line, assuming the encoding is always the same. In the data you're showing, these characters are the byte order mark, which tells us the file's encoding (UTF-16LE, like I mentioned earlier).

However, seeing as how it appears to be plain text, and all you're doing is printing the data, consider just opening it in a plain old text editor (that supports Unicode). Not knowing your operating system, I'm hesitant to suggest a specific one, but if you're on Windows and the file is relatively small, Notepad can do it.

As a side note, __LINE__ will not give you the line number of the file you're reading, it will print the line number of the currently executing script line.

这篇关于PHP文件打开编码问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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