Coupon Codes Generator

I am describing here a way to generate coupon codes with custom configurable parameters.

<?php
function re($l,$d,$pre,$suf,$charset,$del)
{
    $length  = max(1,$l);
    $split   = max(0,$d);
    $suffix  = $suf;
    $prefix  = $pre;

    $splitChar = $del;

    $code = '';
    $charsetSize = count($charset);
    for ($i=0; $i<$length; $i++) {
        $char = $charset[mt_rand(0, $charsetSize - 1)];
        if ($split > 0 && ($i % $split) == 0 && $i != 0) {
            $char = $splitChar . $char;
        }
        $code .= $char;
    }

    $code = $prefix . $code . $suffix;
    return $code;
}

$charset = array();
for ($i = 65; $i <= 90; ++$i) { 
  $charset[] = chr($i); 
}

for ($i = 48; $i <= 57; ++$i) { 
  $charset[] = chr($i); 
}

$format = 'alphanum'; //charset
$del = '-'; //seperator

$l = 6; // total characters
$d = 3; //split at position
$pre = 'XYZ'; //prefix
$suf = '11'; //suffix
$storeCodes = array();
for($i=0;$i<1;$i++)
{
    //echo $i.'<br/>';
    $code = re($l,$d,$pre,$suf,$charset,$del);
    $storeCodes[$i][0] = $code;
}
echo '<pre>';print_r($storeCodes); //array with the coupon codes
?>

Hope this is helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top