Reading data from text files with PHP

Reading data from text files with PHP

Before you read data stored in a file, use the fopen() function to open the file.
To read the data from the file, use fopen() in [e]read mode. After opening the
file, assign the contents of the file to a variable using the file() function. You can then print the contents of the file, then use the fclose() function to close the file after you are finished reading the data.

Function file(string filename [, int use_include_path])
The function reads all the contents of a file into an array. Each line of the file is a
separate element of the array, starting at zero. There is an optional parameter to search for the file in the include_path. Set it to "1", if you want to use it.

Take a look at the contents of file.txt

To print each line of a file you can use the following:

PHP Code:

<?php
 
$filename ="file.txt";
 
$myFile = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file
 
$fcontents = file($myFile);
 
while (list ($line_num, $line) = each ($fcontents)) { // while there are elements in the array
// remove the P tag from each line and print it
echo "<p><b>Line $line_num:</b> " . str_replace("<p>", "", $line) . "<br>\n";
}
 
fclose($myFile);
 
?>

See it in action. ¦ Get complete code.

In the script above the str_replace() function is used to remove the P tags from each line of the file.

Function str_replace (search, replace, subject)
The function replaces all occurrences of the search string it is looking for in the subject string with the replacement string.

To print only a few lines of data from the file, you can use a for-loop. Set five as your limit (the number of lines you want to print) and set up the expressions of the for function to iterate five times.

PHP Code:

<?php
 
$filename ="file.txt";
 
$myFile = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file
 
$fcontents = file($myFile);
 
$limit = 5;
 
for ($i = 0; $i < $limit; $i++){ // while $i is less than 5
 
$line = $fcontents[$i]; // assign value of array element to a variable
 
if($line != ""){ // if the line from the file is not blank print it
echo "$line \n";
}
 
}
 
fclose($myFile);
 
?>

See it in action. ¦ Get complete code.

If you wanted to print the last five lines in the file, the for-loop has to start from the last line in the file instead of the first (remember the file pointer is at the beginning of the opened file).

Note: New data in file.txt is written at the beginning, older data at the end. Normally new data is appended to the end of a file not the beginning. You can create your own function to write data to the beginning of a file instead of the end.

Figure out how many lines of data the file contains by using the count() function to get the total number of lines in the $fcontents array, then assign it to a variable. So if there are 12 total lines in the file, the first line printed will be the 12th line then substract one from the total number of lines to print the 11th etc.

PHP Code:

<?php
 
$filename ="file.txt";
 
$myFile = fopen($filename, "r"); //open the file for reading, file pointer will be at the beginning of the file
 
$fcontents = file($myFile);
 
$totalines = count($fcontents); //how many lines of data the file contains
 
$limit = 5;
 
for ($i = 0; $i < $limit; $i++, $totalines--){
 
$line = str_replace("<p>", "<li>", $fcontents[$totalines]); //remove P tags, replace with LI tag
 
if($line != ""){ // if the line from the file is not blank print it
echo "$line \n";
}
 
}
 
fclose($myFile);
 
?>

See it in action. ¦ Get complete code.