Home >Backend Development >PHP Tutorial >When php reads a txt file, the first line always fails to recognize numbers correctly. What should I do?

When php reads a txt file, the first line always fails to recognize numbers correctly. What should I do?

WBOY
WBOYOriginal
2016-12-01 01:27:201355browse

I read the contents of a txt file line by line,
but the first line of each file is always not recognized correctly.
What should I do?

<code>    $rTxt = fopen( $sTxt  ,"r" );
    
    while( $str = fgets($rTxt) )
    {
    
        $arr = explode( ',' , $str );
        Var_Dump( $arr[0] );
    }
    
    fclose( $rTxt );</code>

txt file content:

<code>1393592460,10680,10660,10650,10720,6040,3.227103E+08,0,0,108,4746,0,0
1393592520,10656,10697,10656,10701,4888,2.612444E+08,0,0,203,6858,0,0
1393592580,10697,10672,10672,10700,1294,6.911194E+07,0,0,281,7086,0,0
1393592640,10671,10667,10661,10675,1706,9.101747E+07,0,0,354,7590,0,0
1393592700,10669,10650,10648,10670,2340,1.246486E+08,0,0,448,8664,0,0
1393592760,10652,10640,10632,10656,1938,1.031578E+08,0,0,545,9282,0,0
1393592820,10641,10657,10641,10662,1746,9.297165E+07,0,0,631,8922,0,0
......</code>

Every time I read it, the length of the first number in the first line of the file is always wrong. There should be an invisible string...:

<code>string(13) "1393592460"
string(10) "1393592520"
string(10) "1393592580"
string(10) "1393592640"

</code>

Reply content:

I read the contents of a txt file line by line,
but the first line of each file is always not recognized correctly.
What should I do?

<code>    $rTxt = fopen( $sTxt  ,"r" );
    
    while( $str = fgets($rTxt) )
    {
    
        $arr = explode( ',' , $str );
        Var_Dump( $arr[0] );
    }
    
    fclose( $rTxt );</code>

txt file content:

<code>1393592460,10680,10660,10650,10720,6040,3.227103E+08,0,0,108,4746,0,0
1393592520,10656,10697,10656,10701,4888,2.612444E+08,0,0,203,6858,0,0
1393592580,10697,10672,10672,10700,1294,6.911194E+07,0,0,281,7086,0,0
1393592640,10671,10667,10661,10675,1706,9.101747E+07,0,0,354,7590,0,0
1393592700,10669,10650,10648,10670,2340,1.246486E+08,0,0,448,8664,0,0
1393592760,10652,10640,10632,10656,1938,1.031578E+08,0,0,545,9282,0,0
1393592820,10641,10657,10641,10662,1746,9.297165E+07,0,0,631,8922,0,0
......</code>

Every time I read it, the length of the first number in the first line of the file is always wrong. There should be an invisible string...:

<code>string(13) "1393592460"
string(10) "1393592520"
string(10) "1393592580"
string(10) "1393592640"

</code>

Most likely it is a utf8 bom character.
Quote SO How to remove multiple UTF-8 BOM sequences

<code>//Remove UTF8 Bom

function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}</code>

Use this function to process your first line and it should be fine.

Want to know more about BOM:
Zhihu - What is the difference between "UTF-8 with BOM" and "UTF-8 without BOM"
Wikipedia

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn