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
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;
  
?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.