Support CSV files with data wrapping a lot of lines

If there is "line" splited on lot of lines we can reach limit of recursion
nesting. It's better to use while instead of recursion.

Closes #1468
This commit is contained in:
drewblin 2020-05-12 11:17:07 +03:00 committed by Adrien Crivelli
parent c4931de1f9
commit 3090c1e73f
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
2 changed files with 20 additions and 21 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Added ### Added
- Support writing to streams in all writers [#1292](https://github.com/PHPOffice/PhpSpreadsheet/issues/1292) - Support writing to streams in all writers [#1292](https://github.com/PHPOffice/PhpSpreadsheet/issues/1292)
- Support CSV files with data wrapping a lot of lines [#1468](https://github.com/PHPOffice/PhpSpreadsheet/pull/1468)
### Fixed ### Fixed

View File

@ -236,12 +236,14 @@ class Csv extends BaseReader
/** /**
* Get the next full line from the file. * Get the next full line from the file.
* *
* @param string $line * @return false|string
*
* @return bool|string
*/ */
private function getNextLine($line = '') private function getNextLine()
{ {
$line = '';
$enclosure = '(?<!' . preg_quote($this->escapeCharacter, '/') . ')' . preg_quote($this->enclosure, '/');
do {
// Get the next line in the file // Get the next line in the file
$newLine = fgets($this->fileHandle); $newLine = fgets($this->fileHandle);
@ -254,15 +256,11 @@ class Csv extends BaseReader
$line = $line . $newLine; $line = $line . $newLine;
// Drop everything that is enclosed to avoid counting false positives in enclosures // Drop everything that is enclosed to avoid counting false positives in enclosures
$enclosure = '(?<!' . preg_quote($this->escapeCharacter, '/') . ')'
. preg_quote($this->enclosure, '/');
$line = preg_replace('/(' . $enclosure . '.*' . $enclosure . ')/Us', '', $line); $line = preg_replace('/(' . $enclosure . '.*' . $enclosure . ')/Us', '', $line);
// See if we have any enclosures left in the line // See if we have any enclosures left in the line
// if we still have an enclosure then we need to read the next line as well // if we still have an enclosure then we need to read the next line as well
if (preg_match('/(' . $enclosure . ')/', $line) > 0) { } while (preg_match('/(' . $enclosure . ')/', $line) > 0);
$line = $this->getNextLine($line);
}
return $line; return $line;
} }