0

私は学習目的でチケット システムを作成していますが、これに似た単純で一意のチケット ID をどのように作成すればよいか考えていました。gD8f-jxS

最初の部分にはランダムなケースの4文字があります

(文字と数字が許可されている場合は、ダッシュがあり、ここでも 3 つのランダムな文字または大文字と小文字の数字が含まれます。

4

1 に答える 1

5
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;
}
于 2012-05-20T23:01:47.727 に答える