1

I want to import a CSV file into my Postgresql database through a PHP page. Is there a way to preprocess the CSV file so as to remove the trailing whitespace other than writing to the database line by line?

M_T
  • 65
  • 1
  • 2
  • 10

1 Answers1

0

Since you already import the data with PHP, you can employ it for preparing the file as well. See trim for removing whitespace at both ends, or rtrim for trailing whitespace.

A simple loop does the job

$fp = fopen('/path/to/file.csv', 'r');
$tmp = fopen('/path/to/tmpfile.csv', 'w');
while (($line = fgets($fp)) !== false) {
    $line = rtrim($line);
    fwrite($tmp, $line);
}

fclose($tmp);
fclose($fp);

// now you can import /path/to/tmpfile.csv
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198