doudun2212 2015-09-17 21:14
浏览 102
已采纳

如何知道.php文件的编码

Whenever we create a file, you can choose the type of coding in different text editors.

My question is, when using php, make reading a file, but the file is not specified in bytes if it is (UTF8, ISO 8859-1, ANSI ...).

But I realize that there are "bits" at the end of that file because the php returns the last STRING with 0. string '' (length = 0)

<?php 
    $leitor = fopen('2.php','rb');
    while(!feof($leitor)){
        var_dump(fread($leitor, 1));
    }
    fclose($leitor);
?>

enter image description here

I believe that this part should contain the type of encoding (UTF-8, ANSI ...)

1) Where is informed on the type of file encoding (UTF8 - 000001, ANSI - 000011)?

2) How do I read "bits" with php actually want to read these last bits that are represented by:

string '' (length = 0)

Because I can do it with the bytes:

function BinString2BitSequence1($mystring) {    
    $result = "";
    $end = strlen($mystring);
    for($i = 0 ; $i < $end; $i++){  
        $result .= str_pad(decbin(ord($mystring[$i])), 8, '0', STR_PAD_LEFT);
    }
    return $result;
}
  • 写回答

2条回答 默认 最新

  • dtjzpg5313 2015-09-17 21:48
    关注

    I believe that this part should contain the type of encoding (UTF-8, ANSI ...)

    You are incorrect. The empty string you're reading at the end of your file is just that: an empty string. It contains no information.

    The feof() function only returns true after a read has already reached the end of the file; as such, it's often not useful. Instead, consider something like:

    while (strlen($str = fread($fh, 1)) > 0) {
       ...
    }
    

    With regard to text encodings, the encoding used for a file is not stored anywhere in the file. It must be inferred by the application. In some cases (e.g, UTF-8 with BOM), this can be detected reliably; in other cases, it may be ambiguous.

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料