每一个电子商务网站,现在有一种或多种类型的优惠/折扣/优惠券系统,给大家分享一下如何在PHP生成唯一的促销/折扣码
- <?php
- /**
- * @param int $no_of_codes//定义一个int类型的参数 用来确定生成多少个优惠码
- * @param array $exclude_codes_array//定义一个exclude_codes_array类型的数组
- * @param int $code_length //定义一个code_length的参数来确定优惠码的长度
- * @return array//返回数组
- */
- function generate_promotion_code($no_of_codes, $exclude_codes_array = '', $code_length = 4) {
- $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $promotion_codes = array(); //这个数组用来接收生成的优惠码
- for ($j = 0; $j < $no_of_codes; $j++) {
- $code = "";
- for ($i = 0; $i < $code_length; $i++) {
- $code.= $characters[mt_rand(0, strlen($characters) - 1) ];
- }
- //如果生成的4位随机数不再我们定义的$promotion_codes函数里面
- if (!in_array($code, $promotion_codes)) {
- if (is_array($exclude_codes_array)) //
- {
- if (!in_array($code, $exclude_codes_array)) //排除已经使用的优惠码
- {
- $promotion_codes[$j] = $code;
- #将生成的新优惠码赋值给promotion_codes数组
- } else {
- $j--;
- }
- } else {
- $promotion_codes[$j] = $code; //将优惠码赋值给数组
- }
- } else {
- $j--;
- }
- }
- return $promotion_codes;
- }
- echo '<h1>Promotion / Discount Codes</h1>';
- echo '<pre>';
- print_r(generate_promotion_code(50, '', 4));
- echo '</pre>';
- ?>
discuz 卡密生成规则设计:DZ2011QSYLP54985V6Q9T
- var $rulekey = array("str" => "\@", "num" => "\#", "full" => "\*");
- var $rulemap_str = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
- var $rulemap_num = "123456789";
- function make($rule = '', $num = 1)
- {
- $makeRule = empty($rule) ? "@@@@####*" : trim($rule);
- if (empty($makeRule)) {
- return -1; //制卡规则为空
- }
- for ($i = 0; $i < $num; $i++) {
- $attempt = 0;
- do {
- if ($attempt >= self::CODE_GENERATION_ATTEMPTS) {
- die('生成卡时跳过重复卡号上限次数用完.');
- }
- $card = $this->createCardNo($makeRule);
- $attempt++;
- } while ($this->exists($card));
- //saveCard($card);
- }
- return true;
- }
- function createCardNo($makeRule)
- {
- if ($ruleReturn = $this->checkrule($makeRule)) {
- $card = $makeRule;
- if (is_array($ruleReturn)) {
- foreach ($ruleReturn AS $key => $val) {
- $search = array();
- foreach ($val AS $skey => $sval) {
- $search[] = '/' . $this->rulekey[$key] . '/';
- }
- $card = preg_replace($search, $val, $card, 1);
- }
- }
- return $card;
- } else {
- return 0;
- }
- }
- function checkrule($rule)
- {
- if (!preg_match("/^[A-Z0-9\@|\#|\*]+$/i", $rule)) {
- return -2;
- }
- $ruleReturn = array();
- foreach ($this->rulekey AS $key => $val) {
- $match = array();
- preg_match_all("/($val){1}/i", $rule, $match);
- $number[$key] = count($match[0]);
- if ($number[$key] > 0) {
- for ($i = 0; $i < $number[$key]; $i++) {
- switch ($key) {
- case 'str':
- $rand = mt_rand(0, (strlen($this->rulemap_str) - 1));
- $ruleReturn[$key][$i] = $this->rulemap_str[$rand];
- break;
- case 'num':
- $rand = mt_rand(0, (strlen($this->rulemap_num) - 1));
- $ruleReturn[$key][$i] = $this->rulemap_num[$rand];
- break;
- case 'full':
- $fullstr = $this->rulemap_str . $this->rulemap_num;
- $rand = mt_rand(0, (strlen($fullstr) - 1));
- $ruleReturn[$key][$i] = $fullstr[$rand];
- break;
- }
- }
- }
- }
- return $ruleReturn;
- }
时间: 2024-10-30 02:31:23