Import CSV with line breaks in real fields

I am using PHP to import a CSV file that comes from an Excel spreadsheet. Some of the fields contain line breaks, so when I open the csv table in the Excel / Open Office table again, it misinterprets where the line break occurs.

Also in my script, using fgetcsv to traverse each line, it breaks lines incorrectly where it shouldn't be.

I could manually clear the data, but a) that will require an age of 10 thousand as a row file, and b) the data will be exported from the existing client part of the software.

Any ideas on how to automatically solve this problem during the import process? I would have thought that delimiting the fields would sort, but that is not the case.

+10
source share
6 answers

I also had this problem, and I did not find a way to read the data correctly.

In my case, it was a one-time import, so I made a script that looked for all line breaks inside the column and replaced it with something like #####. Then I imported the data and replaced it with rows.

If you need regular imports, you can write your own CSV-Parser to deal with this problem. If the text columns are within "", you can treat everything between the two ""as one column (with screening "inside the content).

+3
source

, CSV google, CSV.

parsecsv--PHP: http://code.google.com/p/parsecsv-for-php/

+12

:

nl2br(string);

http://php.net/manual/en/function.nl2br.php

(), , html breaks .

+1

, , {()} , , ,, .

, .

0

, - ppl. ( ) http://csv.thephpleague.com/, NL , .

0

, , , . PHP, .

$parsedCSV = preg_replace('/(,|\n|^)"(?:([^\n"]*)\n([^\n"]*))*"/', '$1"$2 $3"', $parsedCSV);

, , , , , -, , , , . , , ( ).

:

field1,"field2-part1\nfield2-part2",field3

\n , :

field1,"field2-part1 field2-part2",field3

.

, , , , , , ( , ).

0

Source: https://habr.com/ru/post/1534424/


All Articles