Created
February 13, 2013 03:17
-
-
Save cesarmiquel/4941982 to your computer and use it in GitHub Desktop.
Read a CSV, process it and output a CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (count($argv) != 3) { | |
die('Usage: php remap-csv.php <infile> <outfile>'); | |
} | |
// in / out files are $argv[1] and $argv[2] | |
$in = fopen($argv[1], "r"); | |
$out = fopen($argv[2], "w"); | |
$line_num = 1; | |
while (($data = fgetcsv($in, 1000, ",")) !== FALSE) { | |
// test data. In this example test for 4 columns | |
if (count($data) != 4) { | |
echo "Bad line number [$line_num]\n"; | |
print_r($data); | |
} | |
else { | |
// remap some fields 1 and 3 | |
fputcsv($out, array($data[1], $data[3]), ',', '"'); | |
} | |
$line_num++; | |
} | |
// close files | |
fclose($in); | |
fclose($out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment