It is a common task to load data from text files for data processing or storing it into database. In PHP, we can use the file handling and array functions to make it done.
File handling functions
The function fopen()
binds a file resource to a stream and return a file pointer. On the Windows platform, don't forget to escape any backslashes used in the file path.
$file = fopen('c:\\my-folder\\my-data-file.txt', 'r');
The second argument of fopen()
function specifies the access type to the stream. 'r'
means opening for reading only and place the pointer at the beginning of the file.
If want to get a line from file pointer, we can use the fgets()
function. It returns a string from the file pointed to by the file pointer. If an error occurs, false
is returned.
$record = fgets($file);
To check for end-of-file on a file pointer, we can use the feof()
function. true
is returned, if the file pointer is at EOF; otherwise, false
is returned. It is often used to loop over the lines of a file.
while (!feof($file)) {
//file handling ...
//file handling ...
}
After handling the file, the fclose()
function needs to be called to close the file.
fclose($file);
Get data from a string
We can use the array function explode()
to split data fields from a string.
$str = '1001|Carpenter Tia|85|90|Y5';
$data = explode('|', $str);
echo $data[1]; //the output will be: Carpenter Tia
In the example above, suppose we have a text line $str
, we define the separator |
in the first argument of the function explode()
, then it returns a new array for different data fields.
Import data from a text file
Now, it is easy to use the file handling and array functions to import data from a plain text file.
Suppose we have a text data file called data.txt that stores the students' scores of maths and English like this:
ID|Name|Maths|English|Class
1001|Carpenter Tia|85|90|Y5
1002|Holt Evan|88|80|Y5
1003|April Bott|70|92|Y5
1004|Kristen Lefebvre|65|80|Y5
1005|Janice Thrash|72|75|Y5
1006|Jeffrey Lu|68|79|Y5
1007|Shirley Smith|80|78|Y5
1008|Gabe Tang|74|73|Y5
1009|David Pauls|69|75|Y5
1010|Melissa Wong|82|80|Y5
We can use the following codes to extract the students' score records and calculate the total and average scores of maths and English.
$file = fopen('data.txt', 'r');
//ignore the title line
fgets($file);
$mathTotal = 0;
$englishTotal = 0;
$i = 0;
while (!feof($file)) {
$record = fgets($file);
//extract data to an array
$data = explode('|', $record);
//calculate the total score of math
$mathTotal += floatval($data[2]);
//calculate the total score of English
$englishTotal += floatval($data[3]);
$i++;
}
fclose($file);
echo 'Math total: '. $mathTotal.'<br>';
echo 'Math average: '. $mathTotal/$i.'<br>';
echo 'English total: '. $englishTotal.'<br>';
echo 'English average: '. $englishTotal/$i.'<br>';
Since the first line of the text file is for titles, the return value of fgets($file)
is ignored when the function is called for the first time.
References
Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com