PHP CSV Utilities v0.3
Posted: Thu Apr 22, 2010 12:49 pm
Hey guys, so I finally wrapped up PCU (PHP CSV Utilities) v0.3 and I'm looking for some critique.
You can read about how it works on my blog: http://lukevisinoni.com/2010/4/22/php-c ... -released/
But here's a quick example in case you're too lazy to click a link:
Read a CSV file:
Writing CSV data
The code is on Google Code: http://code.google.com/p/php-csv-utils/
I would love to get any kind of ideas for features, critique on interface or bugs, any kind of input would be really appreciated. It's still in pre-1.0, so I'm still really open to interface changes. Thanks guys!
You can read about how it works on my blog: http://lukevisinoni.com/2010/4/22/php-c ... -released/
But here's a quick example in case you're too lazy to click a link:
Read a CSV file:
Code: Select all
// reader auto-detects CSV format (delimiter, quoting style and character, etc.)
$reader = new Csv_Reader('products.csv');
foreach ($reader as $row) {
// do something with $row (numerically indexed)
}
// use more descriptive keys...
$reader = new Csv_Reader('products.csv');
$reader->setHeader(array('id','name','description','price'));
foreach ($reader as $row) {
echo $row['name'] . "<br>";
}
// use explicit format definition
$dialect = new Csv_Dialect(array(
'quoting' => Csv_Dialect::QUOTE_ALL,
'quotechar' => '"',
'delimiter' => ',',
'escapechar' => '"',
'lineterminator' => "\n"
));
$reader = new Csv_Reader('/path/to/file.csv', $dialect);Code: Select all
// writer uses a default format
$writer = new Csv_Writer('/path/to/file.csv');
$writer->writeRows($dataArray);
// or
foreach ($dataArray as $row) {
$writer->writeRow($row);
}
// use explicit format definition
$dialect = new Csv_Dialect(array(
'quoting' => Csv_Dialect::QUOTE_ALL,
'quotechar' => '"',
'delimiter' => ',',
'escapechar' => '"',
'lineterminator' => "\n"
));
$writer = new Csv_Writer('file.csv', $dialect);
// determine file's existing format and use that
$reader = new Csv_Reader('file.csv');
$dialect = $reader->getDialect();
// append file using its existing format...
$writer = new Csv_Writer(fopen('file.csv', 'a'), $dialect);I would love to get any kind of ideas for features, critique on interface or bugs, any kind of input would be really appreciated. It's still in pre-1.0, so I'm still really open to interface changes. Thanks guys!