Solved

# Generate 7 random numbers and 2 random letters

Posted on 2006-05-28
662 Views
I have some code to generate 5 random numbers for a customer ID.  My client would not like it to be 7 numbers ( I can handle that), but they also want two letters to appear anywhere in the string, for a total of 9 characters (7 numbers, 2 letters).  Again, the letters can appear anywhere, random, beginning, end, together, etc.

Does anyone have a function already built for this or have some q & d code?

Thanks!

-Scott
0
Question by:scottparks

LVL 49

Assisted Solution

Try this:

<?php
function custId() {
\$custId = md5(uniqid(rand(), true));
\$custId = substr(preg_replace('/^(\d*[a-z]\d{0,5}[a-z])(.*)\$/e', '"\1".preg_replace("/\D/", "", "\2")', \$custId),0,7);
if(strlen(\$custId) < 7)
return custId(); //regenerate
else
return \$custId;
}

echo custId();
?>
0

LVL 29

Accepted Solution

<?php

function getLetter() {
\$a = 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');
\$s1 = \$a[rand(0, 25)];
return \$s1;
}

function generateCustomerId() {
\$code = rand(1000000, 9999999);
\$code = \$code.getLetter().getLetter();
\$a = str_split(\$code);
shuffle(\$a);
\$customerid = implode('', \$a);
return \$customerid;
}

echo generateCustomerId();
?>
0

## Featured Post

### Suggested Solutions

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …