您正在处理的项目要求您一次处理一行文件。您正在读取一个非常大的文件,在不超出内存限制的情况下读取该文件的唯一方法是一次读取一行。
使用 file() 读取文件
您可以使用 PHP 中的 file() 函数将整个文件一次性读取到数组中。数组元素是文件的各个行。因此,您将能够通过迭代数组来迭代文件中的行。该函数接受三个参数:
文件名:这是您要读取的文件。您还可以提供 URL 作为文件名。flags:这是一个可选参数,可以设置为以下一个或多个常量值:FILE_USE_INCLUDE_PATH、FILE_IGNORE_NEW_LINES 和 FILE_SKIP_EMPTY_LINES。上下文:这也是一个可选参数,用于修改流的行为。
我们将使用 FILE_SKIP_EMPTY_LINES 标志来跳过文件中的所有空行。您可能还想使用 FILE_IGNORE_NEW_LINES 删除各行中的行结尾。
此函数在成功时返回一个包含文件内容的数组,在失败时返回 false。如果文件不存在,您还会收到 E_WARNING 级别错误。这是使用此功能的示例。
登录后复制
上述代码的输出如下所示:
立即学习“PHP免费学习笔记(深入)”;
01. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen02. 03. This eBook is for the use of anyone anywhere in the United States and04. most other parts of the world at no cost and with almost no restrictions05. whatsoever. You may copy it, give it away or re-use it under the terms06. of the Project Gutenberg License included with this eBook or online at07. www.gutenberg.org. If you are not located in the United States, you08. will have to check the laws of the country where you are located before09. using this eBook.10. 11. Title: Pride and Prejudice12. 13. Author: Jane Austen14. 15. Release Date: June, 199816. [Most recently updated: August 23, 2021]
登录后复制
可以看到输出中有一些空行;我们可以使用 FILE_SKIP_EMPTY_LINES 标志来摆脱它们。另外,它可能不明显,但上面的行包含换行符。这就是为什么我们在回显这些行时不必添加自己的换行符。您可以使用 FILE_IGNORE_NEW_LINES 标志来删除空行。
登录后复制
带有这些标志的输出将如下所示:
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() 函数来访问文件句柄。这是我们要运行的代码:
登录后复制
在第一行,我们以只读模式打开文件。然后,我们定义一个函数,它接受 $file_handle 作为参数并返回一行。请注意,我们使用的是 yield 语句,并且我们的函数 get_all_lines() 是一个生成器函数。如果您以前没有使用过 PHP 中的生成器函数,您可能想阅读它们。
如何在 PHP 中使用 Yield 和 Generator
蒙蒂·肖肯2021 年 12 月 26 日
我们在 get_all_lines() 中使用 feof() 函数来检查文件指针是否到达文件末尾。只要我们不在文件末尾,我们就会屈服。通过运行上面的代码,您应该得到以下输出:
1. The Project Gutenberg eBook of Pride and Prejudice, by Jane Austen2. 3. This eBook is for the use of anyone anywhere in the United States and4. most other parts of the world at no cost and with almost no restrictions5. whatsoever. You may copy it, give it away or re-use it under the terms6. of the Project Gutenberg License included with this eBook or online at7. www.gutenberg.org. If you are not located in the United States, you8. will have to check the laws of the country where you are located before9. using this eBook.10. 11. Title: Pride and Prejudice12. 13. Author: Jane Austen14. 15. Release Date: June, 1998 16. [Most recently updated: August 23, 2021]
登录后复制
输出看起来与我们上一节中的相同。这次唯一的区别是您不再面临内存不足的危险。
我之前提到过 fgets() 将允许您一次读取文件的一行,并且它只需要一个指向您要读取的文件的文件指针的参数。在这种情况下,内存消耗取决于行的长度,并且内存不足的可能性很小。
但是,假设您正在阅读一个包含异常长行的文本文件。然后,您可以将可选的第二个参数传递给 fgets() 函数,该函数指定要读取的字符数。然后,它将在停止之前从文件中读取 length – 1 字节。如果遇到新行或文件末尾,它将提前停止。这使您可以更好地控制代码的内存消耗。
最终想法
我在本教程中讨论了两种使用 PHP 逐行读取文件的方法。还有几种方法可以做到这一点,但这两种方法几乎可以满足您的所有需求。当内存消耗不是问题时,请使用 file() 函数,如果您想节省内存,请使用 fgets() 和生成器函数。
以上就是使用PHP逐行读取文件的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/1700822.html