¿Cómo crear código aleatorio con algún prefijo fijo en php? [cerrado]

how to generate a unique Code which will be a 8 Digit code code 1-2 Represent fixed (ZX), Code 3-8 are auto generation of Code. Ex: “ZX23456X”. ?

The format of the unique Code should be as follows: 1) one alphabet. 2) followed by 5 numeric values 3) followed by first two should be fixed "ZX"

preguntado el 28 de agosto de 12 a las 10:08

Have you even attempted this? This is really quite trivial. Why not just "ZX" . $randomNumber? -

how random should it be? if it should be seriously random i suggest you randomize it; verify if it was never earlier generated [database will come handy here] and save to the database. if you expect to see a lot of concurrency you should wrap it in transactions/locking or at least use process id as part of the number. -

4 Respuestas

$start = 'ZX';
$characters = array_merge(range('A','Z'), range('0','9'));
for ($i = 0; $i < 6; $i++) {
    $rand = mt_rand(0, count($characters)-1);
    $start .= $characters[$rand];
}
echo $start;

Respondido 28 ago 12, 10:08

Qué pasa UniqId() ?

function UniqCode($prefix="ZX",$length=8)
{
   return substr(strtoupper(uniqid($prefix)), 0, $length);
}

respondido 29 nov., 12:14

tratan

echo "ZX".substr(md5(uniqid(mt_rand(), true)), 0, 8);

Respondido 28 ago 12, 10:08

Podrías intentar usar <?php echo substr(uniqid('ZX'), 0, 8); ?>, consulte nuestra página, http://uk3.php.net/uniqid

Respondido 28 ago 12, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.