[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

Generate 7 random numbers and 2 random letters

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
scottparks
Asked:
scottparks
2 Solutions
 
RoonaanCommented:
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
 
TeRReFCommented:
<?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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now