Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of file reading and parsing similar to C language by PHP

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces PHP how to achieve similar C language file reading and parsing related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this PHP how to achieve similar C language file reading and parsing function article will have a harvest, let's take a look at it.

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 = 'DGV _ log_file_name _ return;$handle =' DGV _ development _ fopen _ return;$handle = fopen ($log_file_name,'rb') If (FALSE = = $handle) {exit ("Failed to open stream to URL");} / / $stream = fread ($handle, $length); / / read n bytes from the current pointer position of the file / / reset the location of the file pointer. Specify the position of the pointer, after the pointer position is modified. Read the file, and then read / / fseek ($handle,105) from this location; / / fgets represents a line of $error_log_array = [] each time the file is read While (($line = fgets ($handle))! = false) {/ / one line per read / / match appears [1], which is used in the tp log to indicate the fatal error type if (preg_match ("/\ [1\] /", $line)) {$error_log_array [] = $line;}} fclose ($handle)

There are several points to pay attention to:

1. If you are using fwrite, be careful to avoid emptying the contents of the original file. The key is how the fopen is opened. R or w.

If append is used, it is a flag.

2. When fopen, pay attention to determine whether the file is opened successfully. Avoid going into an endless loop when using feof. Because of this function, when passed in is not a pointer, the 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, true is returned. False is not returned at the end. If you happen to pass in an illegal pointer, it will never be the end of the file and will always return false.

Feof () function, when the pointer type is not passed in, there will be an endless loop using the following judgment

While (! feof ($fp)) {}

3. Fread and fgets. If the file is read line by line, fgets is used. Instead of reading by row, use fread () to read.

Note this detail: if there is no more content, return false, that is, in both cases, if the content is empty. False is also 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 determine the end of the file).

4, use the append method (that is, a mark) to open the file, it should be noted that in this way, can not read the contents of the file, can only write the file inside. So fread () on this handle will get false.

To sum up, if you just read the contents of the file, open it only by reading, and if you write new content into it, open it in the way of a

Now we can see why there are multiple modes to distinguish. I used to think it was useless. Now it seems that the way you open the file determines what you can do about the file (add new content or read content).)

This is the end of the article on "how to achieve file reading and parsing functions similar to C language in PHP". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve file reading and parsing functions similar to C language in PHP". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report