Jump to Main Content

Tab-delimited text files w/ PHP

Iterate through the result set

Since the result set is in a variable, we need a way to iterate (read) through the text file for each record. When programming, we use loops for this task. The loop, in this case "while", will grab the content and read through the text file until all records have been read. Inside the loop, we explode the content into a string array and then parse a regular table row and cell for each record.

First, let’s create the "while" loop to grab and read through the text file for each record:

<?php
//create a variable, and assign it the data  stream from names.txt
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
while(!feof($theFile)){
}
?>

Let’s examine the code in detail:

  • First, we create a "while" loop and pass two parameters:
    • First is the negation operator, followed by the feof function, which means for file end of file. This function instructs the loop to keep looping until the end of the file
    • Secondly, we pass in our variable which holds the result set

Here’s how the loop works: The negation operator is a Boolean value, which accept true or false. By passing the feof function and our variable result set as a condition in the "while" loop, we instruct the loop to iterate (read) through the result set until there are no records to read.

Explode the result set

Since our variable contains one string of text from our file, we need to split it into a series of strings using the explode function as shown below:

<?php
//create a variable, and assign it the data  stream from names.txt
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
 $data=explode("\t",  fgets($theFile));
 }
}
?>

Let’s examine the code in greater detail:

  • First, we create a variable, data by using the dollar ($) sign:
    • We assign the variable to the explode function:
    • The explode function will return an array of strings and accepts two parameters:
      • First, we pass a delimiter, in our case, a tabbed character escape sequence (“\t”) which tells us how to handle our array of strings, in our case, tabs
  • Secondly, we use another function, fgets, which is used to one line of text at a time from our text file

Save your file.

Displaying our records

Now that we have our records in a usable format, we need to create a table row and cell to display them. Inside this row, we will use another loop, in this case "for" which will use a condition to determine if we have less than four columns. If this is true, then we will pass our data variable in conjunction with our counter variable, expressed as an array to display data for each record inside a cell.

First, let’s parse a regular table row:

<?php
//create a variable, and assign it the data  stream from names.txt
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
  $data=explode("\t",  fgets($theFile));
  print  "<tr>\n";
 }
}

Let’s examine the code in greater detail:

  • First, we use the print function to parse a regular table row
  • Next, we use a character escape sequence (“\n”) to provide a new line character, which would be present in source view of our browser. This makes reading HTML output easiest to debug and read

Next, we add our loop and parse a regular table cell in conjunction with our data variable:

<?php
//create a variable, and assign it the data  stream from names.txt
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open the  data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
$data=explode("\t",  fgets($theFile));
print  "<tr>\n";
for($i=0;  $i<4; $i++){
print  "<td>" . $data[$i] . "</td>\n";
}
}
?>

Let’s examine the code in greater detail:

  • First, we create a loop which also creates a condition:
    • In the condition, we create a variable, i by using the dollar ($) sign
    • Use a logical operator, less than symbol (<) to make sure our counter variable is less than four columns
    • Increment our counter variable each time through the loop, so we can read the next record

Continuing, inside the loop we do the following:

  • Parse a regular table cell using the print function
  • Concatenate our data variable by using a period (.) which holds our array of strings from our variable and pass the counter variable as a parameter in order to know which record to display
  • Concatenate a closing table cell as well as a character escape sequence, (“\n”), to provide a new line return which is only visible in source view of a browser

Finally, outside the loop, we have the following:

<?php
//create a variable, and assign it the data  stream from names.txt
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
  $data=explode("\t",  fgets($theFile));
  print  "<tr>\n";
   for($i=0;  $i<4; $i++){
   print  "<td>" . $data[$i] . "</td>\n";
  }
  print  "</tr>\n";
}
?>

Let’s examine the code in greater detail:

  • We parse a closing table row using the print function and use a character escape sequence (\n) to provide a new line return which is only visible in the browsers source view

Closing the data source

Since we have completed reading and parsing our records to the browser, we need to close the connection to the data source:

<?php
//create a variable, and assign it the data  stream from names.txt using the fopen function
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
  $data=explode("\t",  fgets($theFile));
  print  "<tr>\n";
   for($i=0;  $i<4; $i++){
   print  "<td>" . $data[$i] . "</td>\n";
  }
  print  "</tr>\n";
 }
//done  with the file, close it
fclose($theFile);
}
?>

We use another function, fclose, which takes one parameter, our original variable which opened the connection to our data source. Save your finished file and preview the results.

Data Output

One slight problem

You might notice from the file above we have an extra row. This is because there’s an extra record in the text file. There are two solutions:

  • Remove the extra row from the text file
  • Check the size of the array. In this method, we ensure our result has only four columns. We nest this check inside our second loop

The second solution is better because as you add additional records, you will encounter the same problem. Add the following check in our script:

<?php
//create a variable, and assign it the data  stream from names.txt using the fopen function
$theFile=fopen("names.txt","r");
//run an error check on the variable to make  sure we received a data stream
if(!$theFile){
 print "<p>Couldn't open  the data file. Try again later.</p>";
}
else{
 while(!feof($theFile)){
  $data=explode("\t",  fgets($theFile));
  if(sizeof($data)==4){
  print  "<tr>\n";
   for($i=0;  $i<4; $i++){
   print  "<td>" . $data[$i] . "</td>\n";
   }
  print  "</tr>\n";
  }
 }
}
//done with the file, close it
fclose($theFile);
}
?>

Let’s examine the code in greater detail:

  • First, we use a condition after the explode function and pass the following parameters:
    • Using the sizeof function, we check the size of the array:
      • If it's equal to four columns, we output the data to the browser

Make sure you properly nest the conditional check outside the loop. Save your file and preview the results.

Finished page

Summary

In this article, you learned the following:

  • Different file formats used on the web
  • How to create a tab-delimited text file using Excel
  • Different data sources, such as databases and text files, specifically, tab-delimited
  • How to read a tab-delimited text file using PHP
  • Built-in functions in PHP which allow us to read and work with text files:
    • open
    • fgets
    • fclose
  • How to open a connection to the text file
  • How to iterate and read through records using a "while" loop
  • How to display each record from our text file using another loop, in this case, "for"
  • How to check for an extra record in a text file using a simple conditional check which determines the size of an array

If you have questions, contact me.

top of page