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条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)