There are two reasons why you might want to read a file line by line with PHP. First one is that the project you are working on requires you to process the file one line at a time. Second is that you are reading a very large file and the only way to read it without going over the memory limit is to do it one line at a time.
Using file()
to Read the File
You can use the file()
function in PHP to read the entire file at once into an array. The array elements are individual lines of the file. So you will be able to iterate over the lines in the file by iterating over the array. The function accepts three parameters:
- filename: This is the file that you want to read. You can also supply a URL as filename.
- flags: This is an optional parameter and can be set to one or more of the following constant values:
FILE_USE_INCLUDE_PATH
,FILE_IGNORE_NEW_LINES
andFILE_SKIP_EMPTY_LINES
. - context: This is also an optional parameter used to modify the behavior of a stream.
We will be using the FILE_SKIP_EMPTY_LINES
flag to skip all the empty lines in a file. You might also want to use FILE_IGNORE_NEW_LINES
to remove line endings from individual lines.
This function returns an array with the file contents upon success and false
upon failure. You will also get an E_WARNING
level error if the file does not exist. Here is an example of using this function.
<?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; } ?>
The output of the above code looks like this:
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]
You can see that there are some empty lines in the output, we can get rid of them by using the FILE_SKIP_EMPTY_LINES
flag. Also, it might not be evident but the lines above include the newline character. That is why we did not have to add a newline character of our own while echoing the lines. You can get rid of the blank lines by using the FILE_IGNORE_NEW_LINES
flag.
<?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; } ?>
The output with those flags will look like this:
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]
Using the file()
function is an easy way to read a file line by line in PHP if you are not worried about memory usage. However, you will have to get more creative if memory usage is an issue because file()
reads the entire file into an array at once.
Using fgets()
to Read the File
Another way to read a file line by line with PHP is to use the fgets()
function. It has one required parameter which is a valid file handle. We will use the fopen()
function to get access to the file handle. Here is the code that we are going to run:
<?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); ?>
On the first line, we open our file in read-only mode. Then, we define a function which accepts a $file_handle
as parameter and gives back a single line. Please note that we are using a yield
statement and our function get_all_lines()
is a generator function. You might want to read about generator functions in PHP if you haven't used them before.
We are using the feof()
function inside get_all_lines()
to check whether our file pointer has reached end-of-file. We only yield as long as we are not at the end of file. You should get the following output by running the code above:
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]
The outputs looks the same as our previous section. The only difference this time is that you are no longer in danger of running out of memory.
I mentioned earlier that fgets()
will allow you to read one line of the file at a time and it only requires a single parameter that points to the file pointer for the file that you want to read. The memory consumption in this case would depend on the length of the line and there is a little chance of you running out of memory.
However, lets say you are reading a text file which contains unusually long lines. You can then pass an optional second parameter to the fgets()
function which specifies the number of characters that you want to read. It will then read length - 1
bytes from the file before stopping. It will stop earlier if it comes across a newline or end of file. This gives you more control over the memory consumption of your code.
Final Thoughts
I have discussed two methods of reading a file line by line with PHP in this tutorial. There are a couple more ways of doing this but these two will meet almost all your needs. Use the file()
function when memory consumption is not an issue and use fgets()
with a generator function if you want to conserve memory.
No comments:
Post a Comment