Page 1 of 1

PHP CSV Utilities v0.3

Posted: Thu Apr 22, 2010 12:49 pm
by Luke
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:

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);
Writing CSV data

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);
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!

Re: PHP CSV Utilities v0.3

Posted: Thu Apr 22, 2010 1:14 pm
by Christopher
I will give it a look and see if I have any comments. I recall that I had some problems with the last version that I emailed you about. Don't remember what they were ... I think it was your detection code. Does this version detect comma vs tab delimited better?

Re: PHP CSV Utilities v0.3

Posted: Thu Apr 22, 2010 4:12 pm
by Luke
It should. I moved the auto-detect functionality into the reader, whereas before you had to do it manually. You can still do it manually if you want, but I can't imagine why you would need to. I just discovered a bug that allows it to "detect" that a space character is the delimiter. It's not supposed to allow the space character as a delimiter. I'm in the process of fixing it, but I can't finish until after work. Other than that it should work great. I really want to get this library up to v1.0 so I would really appreciate testers. I need to find the bugs people! :)

Re: PHP CSV Utilities v0.3

Posted: Thu Apr 22, 2010 6:18 pm
by Christopher
I'll give it a look when I get some time.

Re: PHP CSV Utilities v0.3

Posted: Fri Apr 23, 2010 10:07 am
by pickle
Your function comment for close() is actually for the count() function.

I wonder if count() should implement a cache of some sort, so if I call count() twice, I don't have to read through the entire file twice. Also, it appears that count() will reset the internal line counter - which could cause a problem if I call count() part way through an iteration through the file.

This looks like it works pretty slick - I'll probably be using this in the future.

Re: PHP CSV Utilities v0.3

Posted: Sun Apr 25, 2010 1:11 pm
by Luke
Thank you for the tip about count(). That's a good idea. I have been combing through the code like crazy these last few days and have seen some really sloppy stuff. Some of this code is several years old. I am going to go through and clean up and refactor as much ugly code as possible for v0.4. Then for v0.5 I intend to work on the docs. :) If you do end up using the library, I must warn you that there are most definitely going to be bugs. I haven't used the code in any production code yet. I will also tell you that if you do find bugs I will do my very best to fix them and release a new version as soon as possible. Especially if you submit a patch. If I get a bug report and a patch, I will have a new version out with the fix in it within 24 hours. :)

Re: PHP CSV Utilities v0.3

Posted: Sun Apr 25, 2010 1:13 pm
by Luke
Christopher wrote:I will give it a look and see if I have any comments. I recall that I had some problems with the last version that I emailed you about. Don't remember what they were ... I think it was your detection code. Does this version detect comma vs tab delimited better?
I remember you wanted to be able to provide keys to the reader so that when you loop through the file, you could reference columns by name, which I have added support for :)

Re: PHP CSV Utilities v0.3

Posted: Sun Apr 25, 2010 3:11 pm
by Christopher
Yes, that is important to be able to do. Especially when importing into a database. Thanks. I will take a look when I get some time.