This article describes the implementation of file reading and parsing functions similar to C language in PHP. Share it for your reference, as follows:
$log_file_name = 'D:/static/develop/kuai_zhi//public/Logs/'.date('Ym').'/'.date('d').'_error.log'; //$log_file_name = 'D:/static/develop/kuai_zhi//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 byte lengths later//Reset the location of the file pointer. Specify the position of the pointer, after the pointer position is modified. Read the file, and then starts from this location.//fseek($handle,105); //fgets means one line of the file is read every time$error_log_array = []; while( ($line = fgets($handle) ) !==false){ //A line is read each time //The match appears [1], and this is used to represent the fatal error type in the tp log if(preg_match("/\[1\]/", $line)){ $error_log_array[] = $line; } } fclose($handle);
A few points to note:
1. If it is usedfwrite, be careful to avoid clearing the contents of the original file. The key isfopenopen way. r or w.
If you use the append method, it is the a tag.
2. When fopening, pay attention to determining whether the file is successfully opened. Avoid entering the dead loop when using feof. Because this function, when it is passed in, it is not a pointer, then this function will always return false
The original intention of feof is: to determine whether it is the end of the file. If it is the end, it returns true. Returns false if not the end. If an illegal pointer happens to be passed in, it will never be the end of the file and will always return false.
When the feof() function is not a pointer type, a dead loop will occur using the following judgment
while(!feof($fp)){ }
3. fread and fgets. Read the file by line by line, then use fgets. If not read by line, use fread() to read.
Pay attention to this detail: if there is no more content, false is returned, that is, two cases, if the content inside is empty. False will also be returned. When reading the end of the file, these two functions also return false (no wonder we use feof() so that we won't find this detail, because this function has helped us judge the end of the file)
4. Use the append method (i.e. a mark) to open the file. Note that in this method, if the file content cannot be read, you can only write the file into it. So if you use fread() on this handle, you will get false
In summary, if you just read the content of the file, you will only open it by reading. If you write new content, you will open it by a.
Now I understand why we need to distinguish between multiple modes. I thought it was useless before. Now it seems that the way the file is opened determines what you can do for the file (add new content or read content).
For more information about PHP related content, please check out the topic of this site:PHP file operation summary》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《PHP object-oriented programming tutorial》、《Summary of PHP network programming skills"and"Summary of usage of php strings》
I hope this article will be helpful to everyone's PHP programming.