When php reads a txt file, the first line is always unable to identify the number correctly. what should I do?

Source: Internet
Author: User
I read the contents of a txt file line by line, but the first line of each file is always not recognized correctly. how can I do this? {Code ...} txt file content: {code ...} the length of the first number in the first row of the file is always incorrect each time you read the file. There should be an invisible string ..: {code ...} I read the contents of a txt file line by line,
However, the first line of each file cannot be correctly identified,
What can I do?

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

Txt file content:

1393592460,10680,10660,10650,10720,6040,3.227103E+08,0,0,108,4746,0,01393592520,10656,10697,10656,10701,4888,2.612444E+08,0,0,203,6858,0,01393592580,10697,10672,10672,10700,1294,6.911194E+07,0,0,281,7086,0,01393592640,10671,10667,10661,10675,1706,9.101747E+07,0,0,354,7590,0,01393592700,10669,10650,10648,10670,2340,1.246486E+08,0,0,448,8664,0,01393592760,10652,10640,10632,10656,1938,1.031578E+08,0,0,545,9282,0,01393592820,10641,10657,10641,10662,1746,9.297165E+07,0,0,631,8922,0,0......

The length of the first number in the first row of the file is always incorrect each time you read the file. There should be an invisible string ..:

string(13) "1393592460"string(10) "1393592520"string(10) "1393592580"string(10) "1393592640"

Reply content:

I read the contents of a txt file line by line,
However, the first line of each file cannot be correctly identified,
What can I do?

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

Txt file content:

1393592460,10680,10660,10650,10720,6040,3.227103E+08,0,0,108,4746,0,01393592520,10656,10697,10656,10701,4888,2.612444E+08,0,0,203,6858,0,01393592580,10697,10672,10672,10700,1294,6.911194E+07,0,0,281,7086,0,01393592640,10671,10667,10661,10675,1706,9.101747E+07,0,0,354,7590,0,01393592700,10669,10650,10648,10670,2340,1.246486E+08,0,0,448,8664,0,01393592760,10652,10640,10632,10656,1938,1.031578E+08,0,0,545,9282,0,01393592820,10641,10657,10641,10662,1746,9.297165E+07,0,0,631,8922,0,0......

The length of the first number in the first row of the file is always incorrect each time you read the file. There should be an invisible string ..:

string(13) "1393592460"string(10) "1393592520"string(10) "1393592580"string(10) "1393592640"

It is probably a UTF-8 bom character.
Reference SO How to remove multiple UTF-8 BOM sequences

//Remove UTF8 Bomfunction remove_utf8_bom($text){    $bom = pack('H*','EFBBBF');    $text = preg_replace("/^$bom/", '', $text);    return $text;}

Use this function to process your first line.

Want to know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.