protected function CsvParser::parseLine in Feeds 8.3
Parses a single CSV line.
Parameters
string $line: A line from a CSV file.
bool $in_quotes: Do not use. For recursion only.
string $field: Do not use. For recursion only.
array $fields: Do not use. For recursion only.
Return value
array The list of cells in the CSV row.
3 calls to CsvParser::parseLine()
- CsvParser::getHeader in src/
Component/ CsvParser.php - Returns the header row.
- CsvParser::next in src/
Component/ CsvParser.php - Implements \Iterator::next().
- CsvParser::rewind in src/
Component/ CsvParser.php - Implements \Iterator::rewind().
File
- src/
Component/ CsvParser.php, line 287
Class
- CsvParser
- Parses an RFC 4180 style CSV file.
Namespace
Drupal\feeds\ComponentCode
protected function parseLine($line, $in_quotes = FALSE, $field = '', array $fields = []) {
$line_length = strlen($line);
// Traverse the line byte-by-byte.
for ($index = 0; $index < $line_length; ++$index) {
$byte = $line[$index];
$next_byte = isset($line[$index + 1]) ? $line[$index + 1] : '';
// Beginning a quoted field.
if ($byte === '"' && $field === '' && !$in_quotes) {
$in_quotes = TRUE;
}
elseif ($byte === '"' && $next_byte !== '"' && $in_quotes) {
$in_quotes = FALSE;
}
elseif ($byte === '"' && $next_byte === '"' && $in_quotes) {
$field .= '"';
// Skip the next quote.
++$index;
}
elseif (!$in_quotes && $byte === $this->delimiter) {
$fields[] = $field;
$field = '';
}
elseif (!$in_quotes && $next_byte === '') {
$fields[] = $this
->trimNewline($byte, $field);
$field = '';
}
else {
$field .= $byte;
}
}
// If we're still in quotes after the line is read, continue reading on the
// next line. Check that we're not at the end of a malformed file.
if ($in_quotes && ($line = $this
->readLine())) {
$fields = $this
->parseLine($line, $in_quotes, $field, $fields);
}
// If we're not in quoted after the line is read but the field contains
// data, we are processing a line that did not end.
if (!$in_quotes && $field) {
$fields[] = $field;
}
return $fields;
}