Parsing Comma-Separated Data

Parsing Comma-Separated Data


You have data in comma-separated values (CSV) format'for example, a file exported from Excel or a database'and you want to extract the records and fields into a format you can manipulate in PHP.


If the CSV data is in a file (or available via a URL), open the file with fopen( ) and read in the data with fgetcsv( ) . Figure prints out CSV data in an HTML table.

Reading CSV data from a file

$fp = fopen('sample2.csv','r') or die("can't open file");
print "<table>\n";
while($csv_line = fgetcsv($fp)) {
    print '<tr>';
    for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
        print '<td>'.htmlentities($csv_line[$i]).'</td>';
    print "</tr>\n";
print '</table>\n';
fclose($fp) or die("can't close file");


In PHP 4, you must provide a second argument to fgetcsv( ) that is a value larger than the maximum length of a line in your CSV file. (Don't forget to count the end-of-line whitespace.) In PHP 5 the line length is optional. Without it, fgetcsv( ) reads in an entire line of data. (Or, in PHP 5.0.4 and later, you can pass a line length of 0 to do the same thing.) If your average line length is more than 8,192 bytes, your program may run faster if you specify an explicit line length instead of letting PHP figure it out.

You can pass fgetcsv( ) an optional third argument, a delimiter to use instead of a comma (,). However, using a different delimiter somewhat defeats the purpose of CSV as an easy way to exchange tabular data.

Don't be tempted to bypass fgetcsv( ) and just read a line in and explode( ) on the commas. CSV is more complicated than that, able to deal with field values that have, for example, literal commas in them that should not be treated as field delimiters. Using fgetcsv( ) protects you and your code from subtle errors.

See Also

Documentation on fgetcsv( ) at .

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows