Username / Password :   
LinuxDig.Com Technology Articles
Your Linux News and Resource Site

LinuxDig.Com Linux Article : PHP fgetcsv() Function
Author: HumanX | Tuesday April 20, 2004

PHP contains a wide array of functions for manipulating text files. I have often thought that this is actually one of PHPs strongest attributes. One particular command fgetcsv(), is a powerful function fulfilling the needs of many applications.


Comma Delimited files have been usied for import / exporting data since computer time has began, practically. In the age of XML, the CSV file format still remains strong and is a simple format for storing data and requires less drive space. The CSV format is also used when dealing with large files when parsing takes time an XML is not a good solution.





This does seem like a strange thing to write about but I believe many people do not realize just how strong PHP is in the arena of file manipulation. The capabilities of PHP in this area makes it a great substitute for many applications on the market today as well as other programming languages.

I personally deal in data, large quantities of data. The bulk of this work is done in a comma delimited format or CSV. The CSV format stores its data with unquoted items are considered integers and all else are strings. This format is also interpreted quite well by most spreadsheet applications and it is another reason why it remains popular.

fgetcsv() is an easy command to use and I will give a brief example here.

$fp=fopen("sometextfile.csv","r");
while(! feof($fp)) {
$line[]=fgetcsv($fp,1024);
}

This tiny set of commands reads in a CSV file and stores the item in a multi-dimensional array.

Line 1: Creates a pointer for PHP to read from the file.
Line 2: Reads through the file until the end has reached.
Line 3: $line[] creates an auto incremental key for the first dimension of the array and then fgetcsv($fp,1024) parses the line into another array.
Line 4: End.

Example Text:
"Frank:","Florida"
"Cindy","Pitts"

Example output:
Array 0
Array 1 Frank
Array 2 Florida
Array 1
Array 1 Cindy
Array 2 Pitts


Where is the CSV format used?

Spreadsheet applications
Cell Phone Industry (Sharing of call log files between providers)
Web Logs
Databases (A standard format for sharing information)

The CSV format is used in practically any type of application where data sharing exists.

As you can see the fgetcsv() command allows you to quickly parse files and access your data quickly. fgetcsv can also be modified to work with other delimiters and this is just a simple attribute to the function. For more information, visit http://www.php.net .


"Your Linux News and Resource Site"
©2003 LinuxDig.Com. LinuxDig.Com, LinuxDig and Penguin Initiative are copyrights of LinuxDig.com.
Site Hosted By Digital Environments, Inc. This Website was Created with DE-Web Version 1.9.7.4,
The Fast, Web Based - Website Design Tool, Groupware and Web Hosting System by Digital Environments, Inc.
Groupware:Project Management, Sales Tracking, Web Site Design and News / Blogger all in one package.