| Home > Computer & I'net > Reading a Delimited File us... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
Reading a Delimited File using PHP | |
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 fileValue1, Value2, Value3, Value4, Value5 In the above example we can see a comma between the words: Value1 Value2 until Value 5. Example: TAB delimited fileValue1 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.
Let us go into detail of how these delimited files are used in both cases. Data import into databasesIn 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 databaseIn 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:
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.phpFirst 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 = "c:\\cont.txt";
In the above script we can see a variable called filename which is declared by the PHP syntax: 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 $fd = fopen ($filename, "r");
In the above script we have declared the variable Next we need to declare a variable to hold the contents of the file. Here we will be declaring a variable called $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 ($fd);
In the above script, we are passing the file name to be closed, to the function Next we use a variable called $delimiter = "of";
If you want to specify the delimiter as $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 $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>
Again we open the PHP tag <?
We use echo to display the contents inside the echo $contents;
We close the PHP tag ?>
We use some HTML tags to hook the contents of the split file <br><br>
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 ( $ splitcontents as $color )
In the above script we use a 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> $color\n<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 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:
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 thisSplit File Contents You're output should look like this: Split 1: history
We are done reading a delimited file using PHP ----------------- My Articles are dedicated to my father | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating System·Perl / CGIPHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|