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

PHP file operation: reading files line by line

青灯夜游
Release: 2023-04-10 13:44:02
Original
9329 people have browsed it

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:

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$handle = fopen(&#39;./test.txt&#39;, &#39;r&#39;);                 //打开文件
if (!$handle) {                                     //判断文件是否打开成功
    echo &#39;文件打开失败!&#39;;
}
while (false !== ($char = fgets($handle,1024))) {        //循环读取文件内容
    echo $char."<br>";
}
fclose($handle);                                    //关闭文件
?>
Copy after login

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 $length parameter is omitted, the default is to read Take 1k (i.e. 1024 bytes) length.

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

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

So the above code example can be modified:

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$url = &#39;./test.txt&#39;;
$handle = fopen($url, &#39;r&#39;);                 //打开文件 
$size= filesize($url);                  //计算文件大小
if (!$handle) {                                     //判断文件是否打开成功
    echo &#39;文件打开失败!&#39;;
}
while (false !== ($char = fgets($handle,$size))) {        //循环读取文件内容
    echo $char."<br>";
}
fclose($handle);                                    //关闭文件
?>
Copy after login

The output result is the same as above.

Method 2: Using the fgetss() function

Let’s take a look at the code example:

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$handle = fopen(&#39;./test.txt&#39;, &#39;r&#39;);                 //打开文件
if (!$handle) {                                     //判断文件是否打开成功
    echo &#39;文件打开失败!&#39;;
}
while (false !== ($char = fgetss($handle,1024))) {        //循环读取文件内容
    echo $char."<br>";
}
fclose($handle);                                    //关闭文件
?>
Copy after login

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 $length and $tags. The parameters $file and $length are introduced in the fgets() function. You can learn more about them above. The parameter $tags is 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

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$handle = fopen(&#39;./test.txt&#39;, &#39;r&#39;);                 //打开文件
if (!$handle) {                                     //判断文件是否打开成功
    echo &#39;文件打开失败!&#39;;
}
while (false !== ($char = fgetss($handle,1024,"<h1>,<em>"))) {        //循环读取文件内容
    echo $char."<br>";
}
fclose($handle);                                    //关闭文件
?>
Copy after login

In the above code , we set the value of parameter $tags to "<h1>,<em></em> </h1>", that is, we do not filter <h1></h1> and <em></em> tag, 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!

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