Ok, I have a csv file like this:
14 ; 1234,56 ; 10203 ; "ABC" ; "DFG" ; "Lorem \n ipsum \n dolor sit" \n
15 ; 234,16 ; 10204 ; "ABC" ; "DFG" ; "Lorem \n ipsum \n dolor sit" \n
16 ; 1234,15 ; 10304 ; "CCC" ; "DFG" ; "Lorem ipsum/dolor \n sit amet\consec" \n
and so on…
The file has almost 550000 lines.
How do I replace all \n characters inside double quotes at once?
I’m using PHP 5. Could it be done by preg_replace()?
I don’t know if you’re using fgetcsv(), but you can configure it to recognize individual fields including quoted information.
This way you can read your lines in one at a time and strip the new line characters at the field level rather than having to do an expensive RegEx operation on a large file all at once.
Slightly modified php code example from the documentation (replaced delimiter with ‘;’):
data.txt
This will be recognized as 2 lines instead of 6 because fgetcsv() will recognize the new line characters in the quotes as part of the field and not additional lines of data.