First, we will show that when reading a txt text, we will find that there will be two more characters at the end, because there is a line break, in windows, the line break is represented by \ r \ n. For example, we write a string to the txt File: 127f1"t1.txt & quot; $ str & quot; 123 & quot ;; $ fp1fopen ($ f1, & quot; w & quot;); f...
First demonstrate the effect
When reading a txt text, you will find that there are two more characters at the end because of the existence of line breaks. in windows, the line breaks are represented as \ r \ n.
For example, we write a string to the txt file:
$ F1 = "t1.txt ";
$ Str = "123 ";
$ Fp1 = fopen ($ f1, "w ");
Fwrite ($ fp1, $ str );
Fclose ($ fp1 );
----------------------------------------
Display result:
123
----------------------------------------
Then we test writing to the file twice.
$ F1 = "t1.txt ";
$ Str = "123 ";
$ Fp1 = fopen ($ f1, "w ");
Fwrite ($ fp1, $ str. $ str );
Fclose ($ fp1 );
----------------------------------------
Display result:
123123
----------------------------------------
This is not the result we want to write here. we need to add a line break after writing 123. The line break in windows is \ r \ n.
Test code:
$ Str = "123 ";
$ Fp1 = fopen ($ f1, "w ");
Fwrite ($ fp1, $ str. "\ r \ n". $ str. "\ r \ n ");
Fclose ($ fp1 );
------------------------------------------- Display results
123
123
This is the starting point of a blank line. although you cannot see it, you can click here to write a string.
----------------------------------------
Line breaks are provided when we process text files!
So we need to filter it before we can use a line of strings!
Test code:
The format of the file we read is as follows:
---------------------
123
1234
--------------------
File
$ F1 = "t1.txt ";
$ Str = "123 ";
$ Fp1 = fopen ($ f1, "w ");
Fwrite ($ fp1, $ str. "\ r \ n". $ str. "\ r \ n ");
Fclose ($ fp1 );
$ F1 = "t1.txt ";
$ Fp1 = fopen ($ f1, "r ");
While (! Feof ($ fp1 )){
$ Buffer1 = fgets ($ fp1 );
Echo strlen ($ buffer1 );
Echo"
";
}
Fclose ($ fp1 );
------------------------
Result:
5
5
0
It is proved that the length of string 123 is 5 rather than 3, which is caused by the existence of line breaks. then we can remove the last two characters on the basis of reading!
Code:
$ F1 = "t1.txt ";
$ Str = "123 ";
$ Fp1 = fopen ($ f1, "w ");
Fwrite ($ fp1, $ str. "\ r \ n". $ str. "\ r \ n ");
Fclose ($ fp1 );
$ F1 = "t1.txt ";
$ Fp1 = fopen ($ f1, "r ");
While (! Feof ($ fp1 )){
$ Buffer1 = fgets ($ fp1 );
// The First Conservative method is to calculate the string length obtained at this time, and then extract the first character to the last and second character to the end of the string we want!
$ Buffer1_a = substr ($ buffer1, 0, strlen ($ buffer1)-2 );
// The second method is to directly use the special feature of the substr function to directly intercept strings between 0 and-2.
$ Buffer1_ B = substr ($ buffer1, 0,-2 );
}
Fclose ($ fp1 );
In this way, you can normally use the code to read the desired string.
From cyuyan112233