Skip to content

Instantly share code, notes, and snippets.

@vkdimitrov
Created May 15, 2014 13:56
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 vkdimitrov/0460482ff44baa7fef8d to your computer and use it in GitHub Desktop.
Save vkdimitrov/0460482ff44baa7fef8d to your computer and use it in GitHub Desktop.
generate random fixed length codes
#!/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