首页 > 文章列表 > 使用PHP逐行读取文件

使用PHP逐行读取文件

php 文件读取 逐行读取
136 2023-09-02

您可能想要使用 PHP 逐行读取文件的原因有两个:

  1. 您正在处理的项目要求您一次处理一行文件。
  2. 您正在读取一个非常大的文件,在不超出内存限制的情况下读取该文件的唯一方法是一次读取一行。

使用 file() 读取文件

您可以使用 PHP 中的 file() 函数将整个文件一次性读取到数组中。数组元素是文件的各个行。因此,您将能够通过迭代数组来迭代文件中的行。该函数接受三个参数:

  • 文件名:这是您要读取的文件。您还可以提供 URL 作为文件名。
  • flags:这是一个可选参数,可以设置为以下一个或多个常量值:FILE_USE_INCLUDE_PATHFILE_IGNORE_NEW_LINESFILE_SKIP_EMPTY_LINES
  • 上下文:这也是一个可选参数,用于修改流的行为。

我们将使用 FILE_SKIP_EMPTY_LINES 标志来跳过文件中的所有空行。您可能还想使用 FILE_IGNORE_NEW_LINES 删除各行中的行结尾。

此函数在成功时返回一个包含文件内容的数组,在失败时返回 false。如果文件不存在,您还会收到 E_WARNING 级别错误。这是使用此功能的示例。

<?php

$lines = file('pride-and-prejudice.txt');
$count = 0;

foreach($lines as $line) {
    $count += 1;
    echo str_pad($count, 2, 0, STR_PAD_LEFT).". ".$line;
}

?>

上述代码的输出如下所示:

01. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen
02. 
03. This eBook is for the use of anyone anywhere in the United States and
04. most other parts of the world at no cost and with almost no restrictions
05. whatsoever. You may copy it, give it away or re-use it under the terms
06. of the Project Gutenberg License included with this eBook or online at
07. www.gutenberg.org. If you are not located in the United States, you
08. will have to check the laws of the country where you are located before
09. using this eBook.
10. 
11. Title: Pride and Prejudice
12. 
13. Author: Jane Austen
14. 
15. Release Date: June, 1998
16. [Most recently updated: August 23, 2021]

可以看到输出中有一些空行;我们可以使用 FILE_SKIP_EMPTY_LINES 标志来摆脱它们。另外,它可能不明显,但上面的行包含换行符。这就是为什么我们在回显这些行时不必添加自己的换行符。您可以使用 FILE_IGNORE_NEW_LINES 标志来删除空行。

<?php

$lines = file('pride-and-prejudice.txt', FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
$count = 0;

foreach($lines as $line) {
    $count += 1;
    echo str_pad($count, 2, 0, STR_PAD_LEFT).". ".$line;
}

?>

带有这些标志的输出将如下所示:

01. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen 02. This eBook is for the use of anyone anywhere in the United States and 03. most other parts of the world at no cost and with almost no restrictions 04. whatsoever. You may copy it, give it away or re-use it under the terms 05. of the Project Gutenberg License included with this eBook or online at 06. www.gutenberg.org. If you are not located in the United States, you 07. will have to check the laws of the country where you are located before 08. using this eBook. 09. Title: Pride and Prejudice 10. Author: Jane Austen 11. Release Date: June, 1998 [eBook #1342] 12. [Most recently updated: August 23, 2021] 

如果您不担心内存使用情况,使用 file() 函数是在 PHP 中逐行读取文件的简单方法。但是,如果内存使用成为问题,您将必须发挥更多创意,因为 file() 会立即将整个文件读取到数组中。

使用 fgets() 读取文件

使用 PHP 逐行读取文件的另一种方法是使用 fgets() 函数。它有一个必需参数,即一个有效的文件句柄。我们将使用 fopen() 函数来访问文件句柄。这是我们要运行的代码:

<?php

$file_handle = fopen('pride-and-prejudice.txt', 'r');

function get_all_lines($file_handle) { 
    while (!feof($file_handle)) {
        yield fgets($file_handle);
    }
}

$count = 0;

foreach (get_all_lines($file_handle) as $line) {
    $count += 1;
    echo $count.". ".$line;
}

fclose($file_handle);

?>

在第一行,我们以只读模式打开文件。然后,我们定义一个函数,它接受 $file_handle 作为参数并返回一行。请注意,我们使用的是 yield 语句,并且我们的函数 get_all_lines() 是一个生成器函数。如果您以前没有使用过 PHP 中的生成器函数,您可能想阅读它们。

  • 使用PHP逐行读取文件

我们在 get_all_lines() 中使用 feof() 函数来检查文件指针是否到达文件末尾。只要我们不在文件末尾,我们就会屈服。通过运行上面的代码,您应该得到以下输出:

1. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen
2. 
3. This eBook is for the use of anyone anywhere in the United States and
4. most other parts of the world at no cost and with almost no restrictions
5. whatsoever. You may copy it, give it away or re-use it under the terms
6. of the Project Gutenberg License included with this eBook or online at
7. www.gutenberg.org. If you are not located in the United States, you
8. will have to check the laws of the country where you are located before
9. using this eBook.
10. 
11. Title: Pride and Prejudice
12. 
13. Author: Jane Austen
14. 
15. Release Date: June, 1998 
16. [Most recently updated: August 23, 2021]

输出看起来与我们上一节中的相同。这次唯一的区别是您不再面临内存不足的危险。

我之前提到过 fgets() 将允许您一次读取文件的一行,并且它只需要一个指向您要读取的文件的文件指针的参数。在这种情况下,内存消耗取决于行的长度,并且内存不足的可能性很小。

但是,假设您正在阅读一个包含异常长行的文本文件。然后,您可以将可选的第二个参数传递给 fgets() 函数,该函数指定要读取的字符数。然后,它将在停止之前从文件中读取 length - 1 字节。如果遇到新行或文件末尾,它将提前停止。这使您可以更好地控制代码的内存消耗。

最终想法

我在本教程中讨论了两种使用 PHP 逐行读取文件的方法。还有几种方法可以做到这一点,但这两种方法几乎可以满足您的所有需求。当内存消耗不是问题时,请使用 file() 函数,如果您想节省内存,请使用 fgets() 和生成器函数。

来源:https://code.tutsplus.com/read-a-file-line-by-line-with-php--cms-92971t
上一篇 如何在点击显示菜单时一次只显示一个V菜单? 下一篇 使用 MySQL 视图有哪些限制?

本类最新

查看更多