Detailed explanation of PHP's C-like file reading and parsing implementation functions

黄舟
Release: 2023-03-15 17:40:01
Original
1712 people have browsed it

The example in this article describes how PHP implements file reading and parsing functions similar to C language. Share it with everyone for your reference, the details are as follows:


$log_file_name = 'D:/static/develop/kuai_zhi/acagrid.com/public/Logs/'.date('Ym').'/'.date('d').'_error.log';
//$log_file_name = 'D:/static/develop/kuai_zhi/acagrid.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);//从文件当前指针位置,往后读取n个字节长度
//重置文件指针的位置。指定指针的位置,指针位置修改后。读取文件,后面是从这个位置开始读取了
//fseek($handle,105);
//fgets表示每次读取文件的一行
$error_log_array = [];
while( ($line = fgets($handle) ) !==false){
   //每次读取一行
   //匹配出现[1],tp日志中用这种表示致命错误类型
   if(preg_match("/\[1\]/", $line)){
     $error_log_array[] = $line;
   }
}
fclose($handle);
Copy after login

Several points to note:

1. If you use fwrite, be careful not to clear the contents of the original file. The key is how to open fopen. r or w.

If you use the append method, it is a tag.

2. When fopening, pay attention to determine whether the file is successfully opened. Avoid entering an infinite loop when using feof. Because this function, when passed in is not a pointer, 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, 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 false will always be returned.

feof() function, when the input is not a pointer type, an infinite loop will occur if the following judgment is used


while(!feof($fp)){
}
Copy after login

3, fread and fgets . To read a file line by line, use fgets. Instead of reading by line, use fread() to read.

Pay attention to this detail: if there is no more content, return false, that is, two situations, if the content inside is empty. will also return false. When the end of the file is read, these two functions also return false (no wonder we use feof() so that we will not discover this detail, because this function has already helped us determine the end of the file)

4 . Use the append method (i.e. a mark) to open the file. Please note that in this method, the file content cannot be read and the file can only be written into it. Therefore, fread() on this handle will get false

The summary is that if you only read the contents of the file, just open it for reading. If you want to write new content, use a Open it in

Now I understand why it is necessary 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 with the file (add new content or read content.)

The above is the detailed content of Detailed explanation of PHP's C-like file reading and parsing implementation functions. For more information, please follow other related articles on the PHP Chinese website!

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