What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated Aug 07, 2007 by zynode
Labels: Featured, Phase-Implementation
Examples  
Sample code illustrating how to use the parseCSV class.

General:

$csv = new parseCSV('data.csv');
print_r($csv->data);

Tab delimited, and encoding conversion:

$csv = new parseCSV();
$csv->encoding('UTF-16', 'UTF-8');
$csv->delimiter = "\t";
$csv->parse('data.tsv');
print_r($csv->data);

Auto-detect delimiter character:

$csv = new parseCSV();
$csv->auto('data.csv');
print_r($csv->data);

Modify data in a CSV file:

$csv = new parseCSV();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
$csv->save();

Add row/entry to end of CSV file:

Only recommended when you know the extact sctructure of the file.

$csv = new parseCSV();
$csv->save('data.csv', array('1986', 'Home', 'Nowhere', ''), true);

Convert 2D array to csv data and send headers to browser to treat output as a file and download it:

$csv = new parseCSV();
$csv->output (true, 'movies.csv', $array);

Comment by acumen.dev01, Nov 02, 2007

very goog

Comment by y...@hanez.org, Nov 12, 2007

Thank you for this nice PHP class. It does all i want like expected...

Comment by bradparks, Jan 17, 2008

Thanks so much for your work, and for generously putting this out there as opensource. I needed to do just this myself, and searched PEAR for a CSV parser, but was surprised to see none available. Thanks again!

Comment by Svyatoslav.Zhurovsky, Jul 30, 2008

Thanks!


Sign in to add a comment