I have a CSV file that I want to parse using PHP (don’t want to use php csv functions since they’re too slow for my case). I use preg_replace to select only the columns that I need and write output back to standard out. I have a pattern that look like that
preg_replace("/^\"([^\"]*)\",\"([^\"]*)\"(.*)$/m", "$1;$2", $content);
There are some lines in the CSV that are malformed. Is there a way to skip output for lines that don’t match the pattern above?
You could do it in two steps:
First remove all lines that don’t match your regex:
Then do the original regex replace.
However, you need to be very careful. As soon as you get linebreaks and/or escaped quotes in your CSV fields, these regexes may break and destroy your file.
Or imagine a malformed line like
The regex would now delete both the incomplete and the correct line because
[^\"]*also eats the newlines.