Skip to content

Instantly share code, notes, and snippets.

@evillemez
Created February 25, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evillemez/586b92aac0f3a62bf9bd to your computer and use it in GitHub Desktop.
Save evillemez/586b92aac0f3a62bf9bd to your computer and use it in GitHub Desktop.
simple script to clean a csv file that has an expected structure
#!/usr/bin/env php
<?php
// Note: this script assumes a CSV structure of:
// email,firstName,lastName
//
// The script dudupes by email and trims all fields. Rows
// missing an email are discarded. Validity of email is otherwise
// not checked.
//
// Output is dumped to stdout, so use like so:
//
// ./clean-csv.php /path/to/input.csv > /path/to/output.csv
$raw = file($argv[1]);
$map = [];
foreach ($raw as $row) {
list($email, $firstName, $lastName) = explode(',', $row);
if (!$email) continue;
if (!$firstName && !$lastName) continue;
$map[$email] = [$firstName, $lastName];
}
echo PHP_EOL;
foreach ($map as $email => $names) {
echo implode(',', [
trim($email),
trim($names[0]),
trim($names[1])
]).PHP_EOL;
}
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment