/** * 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) { static $headers = array(); 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; } $fields = $conf['fields'] == 1 ? array($line) : explode($conf['sep'], $line); $nl = array("\n", "\r", "\r\n"); if (in_array($fields[count($fields) - 1], $nl)) { array_pop($fields); } $field_count = count($fields); $last =& $fields[$field_count - 1]; $len = strlen($last); if ($field_count != $conf['fields'] || $conf['quote'] && ($len !== 0 && $last[$len - 1] == "\n" && ($last[0] == $conf['quote'] && $last[strlen(rtrim($last)) - 1] != $conf['quote'] || $last[0] == '=' && $last[1] == $conf['quote'] || preg_match('|^\\s+' . preg_quote($conf['quote']) . '|Ums', $last, $match)))) { fseek($fp, -1 * strlen($line), SEEK_CUR); return File_CSV::readQuoted($file, $conf); } foreach ($fields as $k => $v) { $fields[$k] = File_CSV::unquote($v, $conf['quote']); } if (isset($conf['header']) && empty($headers)) { // read the first row and assign to $headers $headers = $fields; return $headers; } if ($field_count != $conf['fields']) { File_CSV::raiseError("Read wrong fields number count: '" . $field_count . "' expected " . $conf['fields']); return true; } if (!empty($headers)) { $tmp = array(); foreach ($fields as $k => $v) { $tmp[$headers[$k]] = $v; } $fields = $tmp; } return $fields; }
/** * 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; }
/** * 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; } $fields = explode($conf['sep'], $line); if ($conf['quote']) { $last =& $fields[count($fields) - 1]; // Fallback to read the line with readQuoted when guess // that the simple explode won't work right if ($last[strlen($last) - 1] == "\n" && $last[0] == $conf['quote'] && $last[strlen(rtrim($last)) - 1] != $conf['quote'] || count($fields) != $conf['fields']) { $len = strlen($line); fseek($fp, -1 * strlen($line), SEEK_CUR); return File_CSV::readQuoted($file, $conf); } else { $last = rtrim($last); foreach ($fields as $k => $v) { $fields[$k] = File_CSV::unquote($v, $conf['quote']); } } } if (count($fields) != $conf['fields']) { File_CSV::raiseError("Read wrong fields number count: '" . count($fields) . "' expected " . $conf['fields']); return true; } return $fields; }
/** * 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) { static $headers = array(); 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); while ($field_count < $conf['fields']) { if (!($additional_line = fgets($fp, 4096))) { return false; } $line .= $additional_line; $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); return File_CSV::readQuoted($file, $conf); } $fields = File_CSV::unquote($fields, $conf['quote']); if (isset($conf['header']) && empty($headers)) { // read the first row and assign to $headers $headers = $fields; return $headers; } if ($field_count != $conf['fields']) { File_CSV::raiseError("Read wrong fields number count: '" . $field_count . "' expected " . $conf['fields']); return true; } if (!empty($headers)) { $tmp = array(); foreach ($fields as $k => $v) { $tmp[$headers[$k]] = $v; } $fields = $tmp; } return $fields; }