Reading a Delimited File using PHP

Related Articles

Introduction

This article will show you how to read a delimited text file using PHP, which will explain about various aspects of a delimited file, code explanation and source code.

We must be aware of importing data into a database. Data used for importing is normally in the form of a text file. Sometimes these files are called a comma-separated value file, which will normally have a .csv extension.

In either of the files whether it be a text file or a .csv file, all the values are delimited using either a word, letter, number, tab or space. These are known as delimiters. The name of the file is given according to the respective delimiter used. For example if the values are delimited using commas (","), the file will be known as a comma-delimited file. Similarly, if the values are delimited using a delimiter hash ("#") then that file will be called a hash-delimited file.

Let us use an example to see what these delimited files look like.

Example: Comma-delimited file

Value1, Value2, Value3, Value4, Value5

In the above example we can see a comma between the words: Value1 Value2 until Value 5.

Example: TAB delimited file

Value1 Value2 Value3 Value4 Value5

In the above example we are able to see space, the size of one tab between each value.

Now we have seen what a delimited text file looks like, let's have a look at other uses of the delimited file.

  1. Data import into Databases.
  2. Used as a Database

Let us go into detail of how these delimited files are used in both cases.

Data import into databases

In DBMS and RDBMS we create databases and tables. In a huge database consisting of millions of records, in order to transfer data from one database or transport a database from one location to another, data is normally exported into a text file. When we try to export data to a text file, the system will ask which delimiter to be used. When we specify the delimiter to be used, each piece of data stored in the fields will get appended with the delimiter. While importing the data back to the database, the system will ask for the delimiter which is used, based on the delimiter, data gets imported into the database.

Used as a database

In some areas where there is no support for databases, delimited text files are used as a replacement, albeit with limited functionality. Data is directly read from the delimited text file into arrays and manipulated. Usually these types of delimited text files are used in Web applications, where hiring Web hosting with database support is a costly affair. Instead, these delimited text files are used as an alternative to an expensive database.

We have seen what a delimited text file is, what their important uses and applications are. Now we shall go on and see how to read these delimited text files using PHP.

Note: Delimiters are sometimes known as splitters

To execute the script, we need to create two files:

  1. filreread.php
    Filreread.php contains the php code which is used to access the delimited file.
  2. conts.txt
    Conts.txt contains the delimited content.

Now we shall see about what to write about in these two files.

First we shall write code in the file filreread.php

Contents of filreread.php

First we need to open php tag, let us open the tag.

<?

We need to declare a variable and assign the file name and path to it. Here we will be declaring a variable called filename which holds the path of the delimited file.

$filename = "c:\cont.txt";

In the above script we can see a variable called filename which is declared by the PHP syntax: $filename. To that variable we assign the file path: c:\cont.txt. This path is set when you are running the script on a Windows machine.

When you are running your script under UNIX or Linux then you need to set the path as:

$filename = "/home/rasmus/file.txt";

Next we will be using a function called fopen which is used to open the file

$fd = fopen ($filename, "r");

In the above script we have declared the variable $fd to which we are using a variable called fopen, we are passing two parameters to the function. One is the file name and the second is the mode in which the file should be opened. In this script, we open the file in read mode, which is used for reading the file only.

Next we need to declare a variable to hold the contents of the file. Here we will be declaring a variable called contents, to which we will be using a function called fread to read the contents of the file.

$contents = fread ($fd);

In the above script we pass a file name that is to be read. Next we close the file we have opened using the function fclose.

fclose ($fd);

In the above script, we are passing the file name to be closed, to the function fclose.

Next we use a variable called $delimiter which holds the delimiter that is used to split the file. Here we set the delimiter as of,

$delimiter = "of";

If you want to specify the delimiter as # then your variable should look like this

$delimiter = "#";

You can change the delimiter to any text or number you wish, if you want to split a tab-delimited file then your variable should look like this

$delimiter = " ";

Next we will be declaring a variable called splitcontents to that we use explode function, we pass two parameters to the explode function on is the delimiter and the second is the contents .

$splitcontents = explode($delimiter, $contents);

In the above script we use the explode function we pass the variable $delimiter which contains the delimiter and the variable $contents which holds the content of the file. Next we declare a variable called counter which is used inside the loop to find the number of split contents.

$counter = "";

Next we close the PHP tag, to place some HTML tags to identify the general contents and the split contents. Note: without closing the tag you may also specify the HTML tag in the echo.

?>

We shall place some HTML tags now:

<font color="blue" face="arial" size="4">Complete File Contents</font>
<hr>

Again we open the PHP tag

<?

We use echo to display the contents inside the contents variable which hold the entire contents of the file.

echo $contents;

We close the PHP tag

?>

We use some HTML tags to hook the contents of the split file

<br><br>
<font color="blue" face="arial" size="4">Split File Contents</font>
<hr>

We open the PHP tag

<?

As all the contents which are split using the delimiter are stored as an array, we will be using a foreach loop to display all the contents in the split array right from the upper limit to the lower limit.

foreach ( $ splitcontents as $color )
{
$counter = $counter+1;
echo "<b>Split $counter: </b> $colorn<br>";
}

In the above script we use a foreach statement where the split array is passed as a parameter and assigned to the variable $color. Next we see $counter=$counter+1. This variable is used as a serial number , which displays the number of splits and keeps increasing by one as the loop goes on.

Next we display the contents of the array in the loop one by one. We add some HTML content to the echo to identify the split contents.

echo "<b>Split $counter: </b> $colorn<br>"; If you don't require to identify the script, then you can just use: echo "$color";

Next we close the PHP tag.

?>

That's all about the code which is to be placed in the fileread.php file.

Next we shall see what is to be placed in the cont.txt file
Note: cont.txt is the delimited file.
The delimited file may contain any content, which can be split by passing a delimiter. Eg: Here we will be using the contents in the cont.txt

history of the world is the history of the few men who had faith in themselves that faith calls out the divinity with in if u have faith in all the three hundred and thirty millions of your mythological gods and which foreigner has now and again introduced into your midst and still have no faith in yourselves there is no salvation for this.

This is an example of a delimited file.

Full Source code of fileread.php:

<font face="arial" size="3" color="green">Reading a delimited file using PHP by <b>G.Ajaykumar</b> </font> Email: ajaykumar_g@hotmail.com <br><br>

<?
$filename = "c:\info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "of";
$splitcontents = explode($delimiter, $contents);
$counter = "";
?>
<font color="blue" face="arial" size="4">Complete File Contents</font>
<hr>
<?
echo $contents;
?>

<br><br>
<font color="blue" face="arial" size="4">Split File Contents</font>
<hr>
<?
foreach ( $splitcontents as $color )
{

$counter = $counter+1;
echo "<b>Split $counter: </b> $colorn<br>";
}

?>

Full Source code of cont.txt

history of the world is the history of the few men who had faith in themselves that faith calls out the divinity with in if u have faith in all the three hundred and thirty millions of your mythological gods and which foreigner has now and again introduced into your midst and still have no faith in yourselves there is no salvation for this

Split File Contents

You're output should look like this:

Split 1: history
Split 2: the world is the history
Split 3: the few men who had faith in themselves that faith calls out the divinity with in if u have faith in all the three hundred and thirty millions
Split 4: your mythological gods and which foreigner has now and again introduced into your midst and still have no faith in yourselves there is no salvation for this

We are done reading a delimited file using PHP


Publication Date: Tuesday 28th October, 2003
Author: G.ajaykumar View profile

Related Articles