Generate 7 random numbers and 2 random letters

Posted on 2006-05-28
Last Modified: 2008-02-26
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?


Question by:scottparks
    LVL 49

    Assisted Solution

    Try this:

      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
          return $custId;
      echo custId();
    LVL 29

    Accepted Solution


            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);
                    $customerid = implode('', $a);
                    return $customerid;

            echo generateCustomerId();

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    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 …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now