Home >Backend Development >PHP Tutorial >PHP-Code-Solution to the small problem of reading text line breaks_PHP tutorial

PHP-Code-Solution to the small problem of reading text line breaks_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:081676browse

Demonstrate the effect first

When reading a txt text, you will find that there will be two more characters at the end because of the existence of newline characters. The representation method of newline characters in Windows is rn

For example, we write a string to a txt file:

$f1="t1.txt";
$str="123";
$fp1=fopen($f1,"w");
fwrite($fp1,$str);
fclose($fp1);
----------------------------------------

Display results:

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 results:

123123

----------------------------------------

When writing here, it is not the result we want. If we want to break the line after writing 123, we need to add a newline character. The newline character under Windows is: rn

Test code:

$str="123";
$fp1=fopen($f1,"w");
fwrite($fp1,$str."rn".$str."rn");
fclose($fp1);

---------------------------------------------Show results

123

123

This is the starting point of an empty line. Although it cannot be seen, the mouse pointer can click here to write a string.

----------------------------------------

When we process text files, as long as there is a newline, there will be a newline character!

So we have to filter it before we can use one line of string!

Test code:

We read the file format such as:

------------------------

123

1234

--------------------

’s file

$f1="t1.txt";
$str="123";
$fp1=fopen($f1,"w");
fwrite($fp1,$str."rn".$str."rn");
fclose($fp1);

$f1="t1.txt";
$fp1=fopen($f1,"r");
while(!feof($fp1)){
$buffer1=fgets($fp1);
echo strlen($buffer1);
echo "
";
}
fclose($fp1);

------------------------

Results shown:

5
5
0

It proves that the length of string 123 is 5 instead of 3. This is caused by the existence of newline characters. Then we can remove the last two characters based on reading!

Code:

$f1="t1.txt";
$str="123";
$fp1=fopen($f1,"w");
fwrite($fp1,$str."rn".$str."rn");
fclose($fp1);

$f1="t1.txt";
$fp1=fopen($f1,"r");
while(!feof($fp1)){
$buffer1=fgets($fp1);
//The first conservative method is to first calculate the length of the string obtained at this time, and then intercept the first character to the penultimate character to get the string we want!
$buffer1_a=substr($buffer1,0,strlen($buffer1)-2);
//The second method is to directly use the particularity of the substr function to directly intercept the string between 0 and -2
$buffer1_b=substr($buffer1,0,-2);
}
fclose($fp1);

In this way, you can use the code to read the desired string normally


Excerpted from cyuyan112233’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478228.htmlTechArticle First demonstrate the effect. When reading a txt text, you will find that there will be two more characters at the end because there are The existence of newline character, the representation method of newline character in windows is rn. For example, we...
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