Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.0

Request #3935 File_CSV::read should optionally ignore field count
Submitted: 2005-03-23 23:29 UTC
From: crain at fuse dot net Assigned: dufuz
Status: Assigned Package: File_CSV
PHP Version: 4.3.9 OS: Win XP
Roadmaps: (Not assigned)    
Subscription  


 [2005-03-23 23:29 UTC] crain at fuse dot net
Description: ------------ Hi, I would like to see an optional third parameter to File_CSV::read() to instruct the method whether or not to ignore lines with more or fewer fields than calculated by File_CSV::discoverFormat(), e.g. function read($file, &$conf, $ignoreSize = false). Currently, the test at the end of the method results in valid csv data not being returned to the calling script, even though a valid csv file may have lines having varying numbers of elements. If the exploded pieces of a line <> $conf['fields'] (which, being based only on the first five lines of a file, should be only a guideline in any case) the parsed line is not returned. The current behavior breaks PEAR::Contact_AddressBook::importFromFile(), which relies on File_CSV::read() for parsing csv exports from contact managers, which very typically export files of varying numbers of fields. I think it should be up to the calling script to determine how to respond to a mismatch in size. Thanks! Andy Crain

Comments

 [2005-03-23 23:50 UTC] crain at fuse dot net
Oops. I should have said $conf['fields'] is based on first 10 lines of file, not five.
 [2005-03-24 18:01 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2005-03-24 18:07 UTC] crain at fuse dot net
Sorry about that. I knew I was forgetting something: Id: File.php,v 1.6 2005/02/27 10:00:26 Id: CSV.php,v 1.3 2005/02/27 10:00:27
 [2005-03-24 18:18 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2005-03-24 20:57 UTC] crain at fuse dot net
Apologies again: the version numbers I gave previously were from my own repository; in fact, I'm running the most current version of the files: Id: File.php,v 1.28 2005/01/10 13:25:57 mike Exp Id: CSV.php,v 1.18 2005/02/18 11:16:14 dufuz Exp Andy
 [2005-12-15 14:01 UTC] contact at andreass dot net
I would recommend the following approach (I personally need the option to fix the length of fields with empty ones, as Exel produces a different number of empty fields if there are some at the end.): read($file, &$conf, $sizeMismatch='error') if (count($fields) != $conf['fields']) { if( $sizeMismatch == 'fix' ) { return array_merge( $fields, array_fill( count($fields), $conf['fields']-count($fields), '' ) ); } elseif( $sizeMismatch == 'ignore' ) { return $fields; } else { File_CSV::raiseError("Read wrong fields number count: '". count($fields) . "' expected ".$conf['fields']); return true; } }
 [2006-03-18 19:27 UTC] contact at andreass dot net (Andreas Schamberger)
What about incorporating my suggestion into the Package?
 [2006-03-20 11:25 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!