PHP CSV Utilities v0.3

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

PHP CSV Utilities v0.3

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP CSV Utilities v0.3

Post 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?
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP CSV Utilities v0.3

Post 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! :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP CSV Utilities v0.3

Post by Christopher »

I'll give it a look when I get some time.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP CSV Utilities v0.3

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP CSV Utilities v0.3

Post 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. :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP CSV Utilities v0.3

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP CSV Utilities v0.3

Post 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.
(#10850)
Post Reply