Skip to content

Instantly share code, notes, and snippets.

@Moinax
Created June 23, 2014 16:39
Show Gist options
  • Save Moinax/3e3e1807a74a0e08c711 to your computer and use it in GitHub Desktop.
Save Moinax/3e3e1807a74a0e08c711 to your computer and use it in GitHub Desktop.
Coupon generation
/**
* @return string
*/
protected function generateCode()
{
$length = 8;
$code = "";
$possible = "2346789234567891234567892345678923456789234567892345678923456789234567892345678923456789abcdfghjkmnpqrtvwxyzABCDFGHJKLMNPQRTVWXYZ";
$maxlength = strlen($possible);
if ($length > $maxlength) {
$length = $maxlength;
}
$i = 0;
while ($i < $length) {
$char = substr($possible, mt_rand(0, $maxlength - 1), 1);
if (!strstr($code, $char)) {
$code .= $char;
$i++;
}
}
return $code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment