PHP fgets function reads extra spaces to solve

WBOY
Release: 2016-07-29 09:16:15
Original
1965 people have browsed it

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>";
          
}
Copy after login

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";
Copy after login

In this way, it can be read normally, but a space must be added after the last line of string

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!