Home>Article>Backend Development> PHP file operation: reading files line by line

PHP file operation: reading files line by line

青灯夜游
青灯夜游 Original
2021-08-03 17:09:39 9164browse

In the previous article "php file operation - reading files character by character" we introduced a method of reading files: reading files character by character. In this article, we will show you another way to read files: read the file line by line. Let's see how to read the file content line by line and output it. Let's learn together.

There is a text file named "test.txt", the contents of which are:

PHP file operation: reading files line by line

How do we read the contents of the file line by line and Output? Simple, today we will introduce two methods to you.

Method 1: Using the fgets() function

Let’s take a look at the code example:

"; } fclose($handle); //关闭文件 ?>

Output result:

PHP file operation: reading files line by line

Key function analysis:

fgets() function is used to read a line of data from an open file and accepts a required parameter$file(the file being opened) and an optional parameter$length(the number of bytes to be read); if the$lengthparameter is omitted, the default is to read Take 1k (i.e. 1024 bytes) length.

fgets() function encountersnewline character,EOF(read to the end of the file) or reaches the specified length$length-1Stop (the maximum length of the returned string is$length-1bytes).

If we don't know how much data is in the file, we can use thefilesize()function to get it, which can return the size of the specified file (number of bytes).

So the above code example can be modified:

"; } fclose($handle); //关闭文件 ?>

The output result is the same as above.

Method 2: Using the fgetss() function

Let’s take a look at the code example:

"; } fclose($handle); //关闭文件 ?>

Output result:

PHP file operation: reading files line by line

Key function analysis:

The fgetss() function is similar to the fgets() function. Both can read a line of data from the open file. The difference is fgetss() The function filters out HTML and PHP tags from the data.

fgetss() function accepts one required parameter$file, two optional parameters$lengthand$tags. The parameters $file and $length are introduced in the fgets() function. You can learn more about them above. The parameter$tagsis used to specify which tags are not to be removed. Let’s learn more about it through an example:

PHP file operation: reading files line by line

,"))) { //循环读取文件内容 echo $char."
"; } fclose($handle); //关闭文件 ?>

In the above code , we set the value of parameter $tags to "

,

", that is, we do not filter

andtag, so the output result is:

PHP file operation: reading files line by line

Note:After operating the file, remember to use the fclose() function to close the file!

Okay, that’s all. If you want to know anything else, you can click this. → →Basic operation of PHP files

## Recommended:PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of PHP file operation: reading files line by line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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