Created
May 15, 2014 13:56
-
-
Save vkdimitrov/0460482ff44baa7fef8d to your computer and use it in GitHub Desktop.
generate random fixed length codes
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/bin/php | |
<?php | |
if (count($argv) === 1 OR count($argv) < 5) | |
{ | |
echo "Usage: code.php <number_of_codes> <lenght_of_code> <separator> <prefix>\n"; die; | |
} | |
$number_of_codes = $argv[1]; | |
$lenght_of_code = $argv[2]; | |
$separator = $argv[3]; | |
$prefix = $argv[4]; | |
$codes = array(); | |
while (count($codes) <$number_of_codes) | |
{ | |
$code = $prefix.substr(md5(microtime()),rand(0,26),$lenght_of_code); | |
if (in_array($code, $codes)) | |
continue; | |
else | |
array_push($codes, $code); | |
} | |
$codes = array_unique($codes); | |
foreach($codes as $code) | |
{ | |
echo $code.$separator; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment