Tools, FAQ, Tutorials:
Reading One Line of Text from a File in PHP
How To Read One Line of Text from a File in PHP?
✍: FYIcenter.com
If you have a text file with multiple lines, and you want to read those lines one line at a time, you can use the fgets() function. It reads the current line up to the "\n" character, moves the file pointer to the next line, and returns the text line as a string. The returning string includes the "\n" at the end. Here is a PHP script example on how to use fgets():
<?php
$file = fopen("/windows/system32/drivers/etc/services", "r");
while ( ($line=fgets($file)) !== false ) {
$line = rtrim($line);
print("$line\n");
# more statements...
}
fclose($file);
?>
This script will print:
# This file contains port numbers for well-known services echo 7/tcp ftp 21/tcp telnet 23/tcp smtp 25/tcp ...
Note that rtrim() is used to remove "\n" from the returning string of fgets().
⇒ Reading One Character from a File in PHP
⇐ Appending Data to the End of a File in PHP
2016-12-02, ∼4197🔥, 0💬
Popular Posts:
What Is Azure API Management Service? Azure API Management as a turnkey solution for publishing APIs...
How To Merge Cells in a Column? If you want to merge multiple cells vertically in a row, you need to...
Why I am getting "LNK1104: cannot open file 'MSCOREE.lib'" error when building a C++/CLI program? Vi...
How To Use an Array as a Queue in PHP? A queue is a simple data structure that manages data elements...
Where to find EPUB Sample Files? Here is a list of EPUB sample files collected by FYIcenter.com team...