Home >Backend Development >PHP Tutorial >PHP fgets function reads extra spaces to solve
When PHP uses fgets to read a string, it originally stops reading when it encounters a new line, but it actually reads more things after the string. I feel that it is the reason for the new line in Windows. rn, it may be this reason, but in the end it always There is a space, which causes errors when I use strings.
I tried it, mainly using fgetc and case, and found that there are two ascii 0 characters at the end of the string. These two things caused the spaces behind the string.
while(!feof($myfile)) { $data1 = fgets($myfile); $i = strlen($data1); //str_replace(array('\n' ,'\r', '\n\r', ''), 'v', $data1); switch ($data1) { case 0: # code... //echo 0; break; case 10: echo 10; break; default: # code... echo 11; break; } echo $i; //$i++; echo "aa".$data1."aa"; echo "<br>"; echo "aa".substr($data1,0, $i-2)."aa"; echo "<br>"; }
So just use the string interception function and cut off the last two characters.
$i = strlen($data1); echo "aa".substr($data1,0, $i-2)."aa";
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
The above introduces how to solve the problem of reading extra spaces by the php fgets function, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.