使用PHP逐行讀取文件

PHPz
發布: 2023-09-04 18:54:02
原創
1725 人瀏覽過

您可能想要使用 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 等級錯誤。這是使用此功能的範例。


登入後複製

上述程式碼的輸出如下所示:

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 標誌來刪除空白行。


登入後複製

帶有這些標誌的輸出將如下所示:

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逐行讀取文件

我们在 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() 和生成器函数。

以上是使用PHP逐行讀取文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
上一篇:快速提示:在WordPress管理畫面中新增自訂列 下一篇:取得PHP數組的第一個和最後一個元素
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
相關專題
更多>
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!