There are two explanation why you may need to learn a file line by line with PHP. First one is that the venture you’re engaged on requires you to course of the file one line at a time. Second is that you’re studying a really giant file and the one strategy to learn it with out going over the reminiscence restrict is to do it one line at a time.
Utilizing file()
to Learn the File
You need to use the file()
operate in PHP to learn the whole file directly into an array. The array parts are particular person strains of the file. So it is possible for you to to iterate over the strains within the file by iterating over the array. The operate accepts three parameters:
- filename: That is the file that you just need to learn. You can too provide a URL as filename.
-
flags: That is an elective parameter and could be set to a number of of the next fixed values:
FILE_USE_INCLUDE_PATH
,FILE_IGNORE_NEW_LINES
andFILE_SKIP_EMPTY_LINES
. - context: That is additionally an elective parameter used to change the habits of a stream.
We will likely be utilizing the FILE_SKIP_EMPTY_LINES
flag to skip all of the empty strains in a file. You may also need to use FILE_IGNORE_NEW_LINES
to take away line endings from particular person strains.
This operate returns an array with the file contents upon success and false
upon failure. Additionally, you will get an E_WARNING
stage error if the file doesn’t exist. Right here is an instance of utilizing this operate.
<?php $strains = file('pride-and-prejudice.txt'); $rely = 0; foreach($strains as $line) { $rely += 1; echo str_pad($rely, 2, 0, STR_PAD_LEFT).". ".$line; } ?>
The output of the above code seems to be like this:
01. The Undertaking Gutenberg eBook of Satisfaction and Prejudice, by Jane Austen 02. 03. This eBook is for using anybody anyplace in the USA and 04. most different components of the world without charge and with nearly no restrictions 05. by any means. It's possible you'll copy it, give it away or re-use it below the phrases 06. of the Undertaking Gutenberg License included with this eBook or on-line at 07. www.gutenberg.org. If you're not situated in the USA, you 08. must examine the legal guidelines of the nation the place you're situated earlier than 09. utilizing this eBook. 10. 11. Title: Satisfaction and Prejudice 12. 13. Writer: Jane Austen 14. 15. Launch Date: June, 1998 16. [Most recently updated: August 23, 2021]
You may see that there are some empty strains within the output, we will eliminate them through the use of the FILE_SKIP_EMPTY_LINES
flag. Additionally, it won’t be evident however the strains above embrace the newline character. That’s the reason we didn’t have so as to add a newline character of our personal whereas echoing the strains. You may eliminate the clean strains through the use of the FILE_IGNORE_NEW_LINES
flag.
<?php $strains = file('pride-and-prejudice.txt', FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES); $rely = 0; foreach($strains as $line) { $rely += 1; echo str_pad($rely, 2, 0, STR_PAD_LEFT).". ".$line; } ?>
The output with these flags will appear like this:
01. The Undertaking Gutenberg eBook of Satisfaction and Prejudice, by Jane Austen 02. This eBook is for using anybody anyplace in the USA and 03. most different components of the world without charge and with nearly no restrictions 04. by any means. It's possible you'll copy it, give it away or re-use it below the phrases 05. of the Undertaking Gutenberg License included with this eBook or on-line at 06. www.gutenberg.org. If you're not situated in the USA, you 07. must examine the legal guidelines of the nation the place you're situated earlier than 08. utilizing this eBook. 09. Title: Satisfaction and Prejudice 10. Writer: Jane Austen 11. Launch Date: June, 1998 [eBook #1342] 12. [Most recently updated: August 23, 2021]
Utilizing the file()
operate is a straightforward strategy to learn a file line by line in PHP if you’re not frightened about reminiscence utilization. Nonetheless, you’ll have to get extra inventive if reminiscence utilization is a matter as a result of file()
reads the whole file into an array directly.
Utilizing fgets()
to Learn the File
One other strategy to learn a file line by line with PHP is to make use of the fgets()
operate. It has one required parameter which is a legitimate file deal with. We are going to use the fopen()
operate to get entry to the file deal with. Right here is the code that we’re going to run:
<?php $file_handle = fopen('pride-and-prejudice.txt', 'r'); operate get_all_lines($file_handle) { whereas (!feof($file_handle)) { yield fgets($file_handle); } } $rely = 0; foreach (get_all_lines($file_handle) as $line) { $rely += 1; echo $rely.". ".$line; } fclose($file_handle); ?>
On the primary line, we open our file in read-only mode. Then, we outline a operate which accepts a $file_handle
as parameter and offers again a single line. Please word that we’re utilizing a yield
assertion and our operate get_all_lines()
is a generator operate. You may need to examine generator features in PHP if you have not used them earlier than.
We’re utilizing the feof()
operate inside get_all_lines()
to examine whether or not our file pointer has reached end-of-file. We solely yield so long as we aren’t on the finish of file. You must get the next output by operating the code above:
1. The Undertaking Gutenberg eBook of Satisfaction and Prejudice, by Jane Austen 2. 3. This eBook is for using anybody anyplace in the USA and 4. most different components of the world without charge and with nearly no restrictions 5. by any means. It's possible you'll copy it, give it away or re-use it below the phrases 6. of the Undertaking Gutenberg License included with this eBook or on-line at 7. www.gutenberg.org. If you're not situated in the USA, you 8. must examine the legal guidelines of the nation the place you're situated earlier than 9. utilizing this eBook. 10. 11. Title: Satisfaction and Prejudice 12. 13. Writer: Jane Austen 14. 15. Launch Date: June, 1998 16. [Most recently updated: August 23, 2021]
The outputs seems to be the identical as our earlier part. The one distinction this time is that you’re not in peril of operating out of reminiscence.
I discussed earlier that fgets()
will let you learn one line of the file at a time and it solely requires a single parameter that factors to the file pointer for the file that you just need to learn. The reminiscence consumption on this case would rely upon the size of the road and there’s a little likelihood of you operating out of reminiscence.
Nonetheless, let’s imagine you’re studying a textual content file which comprises unusually lengthy strains. You may then cross an elective second parameter to the fgets()
operate which specifies the variety of characters that you just need to learn. It can then learn size - 1
bytes from the file earlier than stopping. It can cease earlier if it comes throughout a newline or finish of file. This offers you extra management over the reminiscence consumption of your code.
Remaining Ideas
I’ve mentioned two strategies of studying a file line by line with PHP on this tutorial. There are a pair extra methods of doing this however these two will meet nearly all of your wants. Use the file()
operate when reminiscence consumption isn’t a problem and use fgets()
with a generator operate if you wish to preserve reminiscence.