PHP implements file reading and parsing functions similar to the C language,

Source: Internet
Author: User

PHP implements file reading and parsing functions similar to the C language,

This example describes how PHP implements file reading and parsing functions similar to the C language. We will share this with you for your reference. The details are as follows:

$ Log_file_name = 'd:/static/develop/kuai_zhi/acloud d.com/public/logs/'.date ('ym '). '/'. date ('D '). '_ error. log'; // $ log_file_name = 'd:/static/develop/kuai_zhi/acw.d.com/public/logs/201701/19_error.log'{if (! File_exists ($ log_file_name) return; $ handle = fopen ($ log_file_name, 'rb'); if (FALSE ===$ handle) {exit ("Failed to open stream to URL");} // $ stream = fread ($ handle, $ length); // from the current pointer position of the file, read n Bytes later // reset the position of the file pointer. Specify the pointer position. Read the file, followed by the read from this location // fseek ($ handle, 105); // fgets indicates that each row of the file is read $ error_log_array = []; while ($ line = fgets ($ handle ))! = False) {// read a row at a time // match [1]. In tp logs, this indicates the fatal error type if (preg_match ("/\ [1 \]/", $ line) {$ error_log_array [] = $ line ;}} fclose ($ handle );

Notes:

1. If fwrite is used, avoid clearing the content of the original file. The key is the fopen open method. R or w.

If the append method is used, it is a mark.

2. When fopen is enabled, check whether the file is successfully opened. Avoid endless loops when using feof. Because this function will always return false if it is passed in instead of a pointer.

The intention of feof is to determine whether it is the end of the file. Returns true if it is the end. Otherwise, false is returned. If an Invalid Pointer is passed in, the pointer is never the end of the file and false is always returned.

Feof () function, when passed in is not a pointer type, the following judgment will show an endless loop

while(!feof($fp)){}

3. fread and fgets. Fgets is used to read a row of a file. Instead of reading data by row, use fread () to read data.

Note the following details: If there is no more content, false is returned, that is, if the content is empty. Otherwise, false is returned. When the end of the file is read, the two functions also return false (no wonder we use feof () so that we won't find this detail, because this function has helped us determine the end Of the file)

4. Use the append method (mark a) to open the file. Note that if you cannot read the file content in this method, you can only write the file to it. Therefore, performing fread () on this handle will get false.

In summary, if you only want to read the file content, you only need to read it. If you want to write new content, you can open it in.

Now we can see why we need to distinguish between multiple modes. I used to think it was useless. Now it seems that the method of opening a file determines what you can do for the file (add new content or read content .)

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.