php – How to generate a serial key that contains a prefix in the first 5 characters?

Question:

I have a script in PHP that generates a " serial key ", but I need this script to generate this " serial key " with a pattern at the beginning, as if it were a prefix.

The code I already have is:

<?php
$chars = array(0,1,2,3,4,5,6,7,8,9,'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');
$serial = '';
$max = count($chars)-1;
for($i=0;$i<20;$i++){
    $serial .= (!($i % 5) && $i ? '-' : '').$chars[rand(0, $max)];
}
echo $serial;
?>

Which generates a serial like this:

  • "HJMW0-5RRXT-CS853-BD888"

I want the first 5 characters (the first set before the " – ") of all generated serials to be equal and the others to be just numbers, no letters, like this:

  • "XYAMN-97354-81037-01936"

  • "XYAMN-75903-81938-01936"

Answer:

I don't know if I understood correctly, but the prefix of your serial, will it be a fixed text that you will choose? If so, you can make a very simple change to your code, which is to take all the letters out of the array, and decrease the for count from 20 to 15 digits, and put your prefix text manually, like this:

$chars = array(0,1,2,3,4,5,6,7,8,9);
$serial = '';
$max = count($chars)-1;
for($i=0;$i<15;$i++){
    $serial .= (!($i % 5) && $i ? '-' : '').$chars[rand(0, $max)];
}
echo 'XYAMN-'.$serial;

Now if you want the prefix to be random letters, you can separate into 2 arrays, one for numbers and one for letters, and create 2 for . But I would suggest a simplified way without arrays using php's chr function, like this:

for($i=0; $i<5; $i++){
    $prefixo .= strtoupper(chr(rand(97, 122))); //97 é o codigo ascii para 'a' e 122 para z
}

for($i=0; $i<15; $i++){
    $serial .= (!($i % 5) && $i ? '-' : '').rand(0, 9);
}
echo $prefixo.'-'.$serial;

What if you create functions and classes to make your serial generator much more dynamic. For example, make a modifier for the digit separator, use a single for for both situations, and so on. For example:

function Serial($tipo = '', $qtdigitos = 5, $qtdbaterias = 4, $separador = '-') {
    $qtdtotal = $qtdbaterias * $qtdigitos;
    $letrasnumeros = array_merge(range(0,9), range('A', 'Z')); // Cria um array de letras e numeros de forma simplificada

    for($i=0; $i < $qtdtotal; $i++){

        if ($tipo == 'numeros') { $digito = rand(0, 9); } 
        else if($tipo == 'letras') { $digito = chr(rand(65, 90)); }   //65 é o codigo ascii para 'A' e 90 para 'Z'
        else { $digito = $letrasnumeros[rand(0, count($letrasnumeros) - 1)]; }

        $serial .= (!($i % $qtdigitos) && $i ? $separador : '').$digito;
    }
  return $serial;
}

And then you can use the function in several ways:

// Retorna serial com letras e numeros, 
// Ex: RQ4BD-1NSBA-PXUD9-DOKS6
echo Serial(); 

// Retorna serial só com números, 
// Ex: 07295-31860-33824-63832
echo Serial('numeros'); 

// Retorna serial só com letras, 
// Ex: CDMIC-AXLET-BRMGW-QUVWL
echo Serial('letras'); 

// Retorna serial só com números mas quantidade diferente de caracteres, 
// Ex: 339-671-633-081-731-120
echo Serial('numeros', 3, 6); 

// Utilizar separadores diferentes,
// Ex: 2CQHJ.SF1E6.D5SOG.UA10K
echo Serial('aleatorio', 5, 4, '.');

// Juntar formas e quantidades diferentes,
// Ex: AMQGUUY-82468-32482-84190
echo Serial('letras', 7, 1).'-'.Serial('numeros', 5, 3);

// Juntar texto fixo com serial
// Ex: XYAMN-16697-17479-56095
echo 'XYAMN-'.Serial('numeros', 5, 3);

Hope this helps

Scroll to Top