PHP fgets function reads extra whitespace to resolve

Source: Internet
Author: User
PHP with Fgets read the string, it would have encountered a newline stop reading, but the actual will be in the back of the string to read something, I feel is the cause of Windows line, \ r \ n, this may be the reason, there is always a space, resulting in the use of the string I always error.

In a try, mainly using fgetc, and case, found that the end of the string has two ASCII 0 characters, these two things caused the string after the space.

while (!feof ($myfile)) {        $data 1 = fgets ($myfile);        $i = strlen ($data 1);        Str_replace (Array (' \ n ', ' \ R ', ' \n\r ', '), ' V ', $data 1);        Switch ($data 1) {case             0:                  # code ...             echo 0;                  break;             Case Ten:             Echo ten;             break;             Default:                  # code ...             echo one;                  break;        }                    echo $i;          $i + +;          echo "AA". $data 1. " AA ";                    echo "
"; echo "AA". substr ($data 1,0, $i-2). " AA "; echo "
"; }

So the decisive use of string intercept function, cut off the last two characters just fine.

$i = strlen ($data 1); echo "AA". substr ($data 1,0, $i-2). " AA ";

This will be read normally, but the last line of string will be followed by a space

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP fgets function to read the extra space to solve, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.