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

如何知道.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;
}

图片转代码服务由CSDN问答提供 功能建议

每当我们创建文件时,您都可以在不同的文本编辑器中选择编码类型。

我的问题是,当使用php时,会读取一个文件,但如果是(UTF8,ISO 8859-1,ANSI ...),则不以字节为单位指定文件。 \ n

但我意识到该文件末尾有“位”,因为php返回的最后一个STRING为0. string''(length = 0)

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

我认为这部分应该包含编码类型(UTF-8,ANSI ...)

1)在哪里通知文件编码类型(UTF8 - 000001,ANSI - 000011)?

2)如何用PHP实际读取“位” 想要读取由以下表示的最后一位:

string''(length = 0)

因为我可以用字节来做:

  function BinString2BitSequence1($ mystring){
 $ result =“”; 
 $ end = strlen($ mystring); 
 for($ i = 0;  $ i&lt;  $结束;  $ i ++){
 $ result。= str_pad(decbin(ord($ mystring [$ i])),8,'0',STR_PAD_LEFT); 
} 
返回$ 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.

    点赞 打赏 评论
  • doubei2340 2015-09-17 21:21

    Here you have it take a look at this function This function will output encoding based on string just pass your whole text

    点赞 打赏 评论

相关推荐 更多相似问题