


When php reads a txt file, the first line always fails to recognize numbers correctly. What should I do?
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
