简体   繁体   English

PHP票证ID生成

[英]PHP Ticket ID Generation

I am creating a ticket system for learning purposes, and I was wondering how would I create a simple unique ticket ID that would be similiar to this: gD8f-jxS 我正在创建一个用于学习目的的票务系统,我想知道如何创建一个与此相似的简单唯一票证ID: gD8f-jxS

It would have 4 characters of random case in the first part 在第一部分中它将有4个随机大小写的字符

(letters and numbers are allowed, then it would have a dash, and again 3 random letters or numbers of any case. (允许使用字母和数字,然后会有破折号,再加上3个随机字母或数字)。

public function generateCode(){

    $unique =   FALSE;
    $length =   7;
    $chrDb  =   array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');

    while (!$unique){

          $str = '';
          for ($count = 0; $count < $length; $count++){

              $chr = $chrDb[rand(0,count($chrDb)-1)];

              if (rand(0,1) == 0){
                 $chr = strtolower($chr);
              }
              if (3 == $count){
                 $str .= '-';
              }
              $str .= $chr;
          }

          /* check if unique */
          //$existingCode = UNIQUE CHECK GOES HERE  
          if (!$existingCode){
             $unique = TRUE;
          }
    }
    return $str;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM