Last active
March 3, 2018 00:06
-
-
Save markisatacomputer/378f51007905d79fc22fc59d2bc5fd03 to your computer and use it in GitHub Desktop.
php shell script for ordering email addresses
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
#!/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"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pbpaste | ~/scripts/sort-email.php | pbcopy