Skip to content

Instantly share code, notes, and snippets.

@markisatacomputer
Last active March 3, 2018 00:06
Show Gist options
  • Save markisatacomputer/378f51007905d79fc22fc59d2bc5fd03 to your computer and use it in GitHub Desktop.
Save markisatacomputer/378f51007905d79fc22fc59d2bc5fd03 to your computer and use it in GitHub Desktop.
php shell script for ordering email addresses
#!/usr/local/bin/php
<?php
/**
*
* @param [type] $address1 [description]
* @param [type] $address2 [description]
* @return [type] [description]
*/
function byemail($address1, $address2) {
return strcasecmp(
preg_replace('/^.*<(.*)>.*$/', '${1}', $address1),
preg_replace('/^.*<(.*)>.*$/', '${1}', $address2)
);
}
//| Get options
$options = array();
foreach ($argv as $i => $arg) {
if (strpos($arg, '-') === 0) {
$options[] = $arg;
array_splice($argv, $i, 1);
}
}
//| Test for standard iput
stream_set_blocking(STDIN, 0);
$addresses = fgetcsv(STDIN);
//| If not, get addresses from args
if (!is_array($addresses) && isset($argv[1])){
$addresses = explode(',', $argv[1]);
//| No input!!
} else if (!is_array($addresses)) {
print "Please give me some emails to order.\n";
}
// Sort by email
if (in_array('-email', $options)) {
usort($addresses, "byemail");
} else {
// Sort by firstname
natcasesort($addresses);
}
// give it back to the user!
foreach ($addresses as $address) {
print "$address,\n";
}
?>
@markisatacomputer
Copy link
Author

pbpaste | ~/scripts/sort-email.php | pbcopy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment