Jump to content

Generating Serial Number


ivoilic

Recommended Posts

So I wrote this code to generate a 12 character long serial number looking something like this: 123a-34se-sdf3

I am new to php and really bad at using loops.  If some one could explain to me some method of repeating the action rather than having to copy and paste.

 

		$ser_num = array("1","2","3","4","5","6","7","8","9","0","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");
$num1 = rand(0,count($ser_num)-1);
$numf1 = wordwrap($ser_num[$num1],1,"\n", true);
$num2 = rand(0,count($ser_num)-1);
$numf2 = wordwrap($ser_num[$num2],1,"\n", true);
$num3 = rand(0,count($ser_num)-1);
$numf3 = wordwrap($ser_num[$num3],1,"\n", true);
$num4 = rand(0,count($ser_num)-1);
$numf4 = wordwrap($ser_num[$num4],1,"\n", true);

$num5 = rand(0,count($ser_num)-1);
$numf5 = wordwrap($ser_num[$num5],1,"\n", true);
$num6 = rand(0,count($ser_num)-1);
$numf6 = wordwrap($ser_num[$num6],1,"\n", true);
$num7 = rand(0,count($ser_num)-1);
$numf7 = wordwrap($ser_num[$num7],1,"\n", true);
$num8 = rand(0,count($ser_num)-1);
$numf8 = wordwrap($ser_num[$num8],1,"\n", true);

$num9 = rand(0,count($ser_num)-1);
$numf9 = wordwrap($ser_num[$num9],1,"\n", true);
$num10 = rand(0,count($ser_num)-1);
$numf10 = wordwrap($ser_num[$num10],1,"\n", true);
$num11 = rand(0,count($ser_num)-1);
$numf11 = wordwrap($ser_num[$num11],1,"\n", true);
$num12 = rand(0,count($ser_num)-1);
$numf12 = wordwrap($ser_num[$num12],1,"\n", true);

$serial = ''.$numf1.''.$numf2.''.$numf3.''.$numf4.'-'.$numf5.''.$numf6.''.$numf7.''.$numf8.'-'.$numf9.''.$numf10.''.$numf11.''.$numf12.'';

Link to comment
https://forums.phpfreaks.com/topic/265715-generating-serial-number/
Share on other sites

Try something like this:

<?PHP

  function generate_serial() {
    //### Possible characters in the serial
    $characters = array("1","2","3","4","5","6","7","8","9","0","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");
    
    //### Create final serial variable
    $final_serial = '';
    
    //### Loop 12 times to get 12 characters
    for($i=1; $i<=12; $i++) {
    
      //### Add the character to the serial
      $final_serial .= $characters[array_rand($characters)];
    
      //### After each 4 characters add a hypen, except the last 4
      if($i%4 == 0 && $i!=12) {
        $final_serial .= '-';
      }
    }
    
    //### Return the final serial
    return $final_serial;
  }
  
  $serial = generate_serial();
  
  echo $serial;
  
?>

There is a possibility of duplicates, so if that's a concern you'll have to write code to check for them, but this would fit the description of what you want, I believe.

 

<?php
$digits = range(0, 9);
$letters = range('A', 'Z');
$characters = array_merge($digits, $letters);

$serial = array();
for( $i = 0; $i < 4; $i++ ) {
        $serial[$i] = '';
for ( $n = 0; $n < 4; $n++ ) {
	$serial[$i] .= $characters[array_rand($characters)];
}
}

$serial_full = implode('-', $serial);
echo $serial_full;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.