示例#1
0
 /**
  * Reads a "row" from a CSV file and return it as an array
  *
  * @param string $file The CSV file
  * @param array  &$conf The configuration of the dest CSV
  *
  * @return mixed Array or false
  */
 function read($file, &$conf)
 {
     if (!($fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ))) {
         return false;
     }
     // The size is limited to 4K
     if (!($line = fgets($fp, 4096))) {
         return false;
     }
     if ($conf['fields'] === 1) {
         $fields = array($line);
         $field_count = 1;
     } else {
         $fields = explode($conf['sep'], $line);
         $field_count = count($fields);
     }
     $real_field_count = $field_count - 1;
     $check_char = $fields[$real_field_count];
     if ($check_char === "\n" || $check_char === "\r") {
         array_pop($fields);
         --$field_count;
     }
     $last =& $fields[$real_field_count];
     if ($field_count !== $conf['fields'] || $conf['quote'] && ($last !== '' && ($last[0] === $conf['quote'] && $last[strlen(rtrim($last)) - 1] !== $conf['quote'] || $last[0] === '=' && $last[1] === $conf['quote'])) || count(explode(',', $line)) > $field_count) {
         fseek($fp, -1 * strlen($line), SEEK_CUR);
         $fields = File_CSV::readQuoted($file, $conf);
         $fields = File_CSV::_processHeaders($fields, $conf);
         return $fields;
     }
     $fields = File_CSV::unquote($fields, $conf['quote']);
     if ($field_count != $conf['fields']) {
         File_CSV::raiseError("Read wrong fields number count: '" . $field_count . "' expected " . $conf['fields']);
         return true;
     }
     $fields = File_CSV::_processHeaders($fields, $conf);
     return $fields;
 }