I am importing data from a CSV file using fgetcsv(), which yields an array for each row. I then implode() this array and use mysql_real_escape_string() to create a string for use in a mysql query. It ends up looking like this:
\"2342352353\",\" \",\"\"
Some of the CSV files I am reading in have up to 200+ columns (rediculous, but true), so the insert statements become extremely long. To make them not so lengthy, I want to get rid of any spaces or blanks in the insert statement. So, the statement above should become:
\"2342352353\",,
So I am looking at 2 ways to solve this. First, I could walk through the array, and use trim() to find any entries with spaces and make them blank. Then I could do a str_replace() on the resulting string like so:
for ($x=0; $x<sizeof($data); $x++) {
$data[$x] = trim($data[$x]) == '' ? '' : $data[$x];
}
$insert_string = '\\"' . mysql_real_escape_string(implode('","', $data)) . '\\"';
str_replace('\"\"', '', $insert_string);
Second approach is to skip walking through the array and just use regex to remove any occurrences of \" \", but there could be any number of spaces in the middle of the delimited quotes.
My question is 1st and foremost, how do I do this using regex? And secondly, which way would be faster?
Once I get the regex answer, I will do some benchmarking and will update with the results.
Thanks!
What about this regex?
\\"\s*\\"Used in your code above:
preg_replace('/\\\"\s*\\\"/', '', $insert_string);The
\s*matches any number of whitespace characters.And I’d eat my hat if regex was slower than iterating through your CSV file…