Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:01:07+00:00 2026-05-25T19:01:07+00:00

I want to generate a unique 4-6 char long AlphaNumeric string to save in

  • 0

I want to generate a unique 4-6 char long AlphaNumeric string to save in db with each record(user). The db field has a unique index, so trying to save a pre-existing string generates an error. Right now I am generating a random string and using try-catch, so when adding a new record if it throws an exception, I generate another random string and attempt to save again, and the code keep trying until it adds a record successfully. This whole solution not only looks heavy but also ugly, so I want to change it. I am interested in an elegant solution, so any help/guidance is welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:01:08+00:00Added an answer on May 25, 2026 at 7:01 pm

    With the given information :

    • id must be unique
    • id must not be numeric
    • id must not represent a sequential series
    • id will not be input by the user

    The PHP function uniqid is exactly what you need. Though it returns a 13 character long hexadecimal value.


    ** Edit **

    Yes, uniqid will return a seamingly sequential number, but we can get around this easily. Consider this code

    class IDGenerator {
       //const BIT_MASK = '01110011';
    
       static public function generate() {
    
          $id = uniqid();
    
          $id = base_convert($id, 16, 2);
          $id = str_pad($id, strlen($id) + (8 - (strlen($id) % 8)), '0', STR_PAD_LEFT);
    
          $chunks = str_split($id, 8);
          //$mask = (int) base_convert(IDGenerator::BIT_MASK, 2, 10);
    
          $id = array();
          foreach ($chunks as $key => $chunk) {
             //$chunk = str_pad(base_convert(base_convert($chunk, 2, 10) ^ $mask, 10, 2), 8, '0', STR_PAD_LEFT);
             if ($key & 1) {  // odd
                array_unshift($id, $chunk);
             } else {         // even
                array_push($id, $chunk);
             }
          }
    
          return base_convert(implode($id), 2, 36);
       }
    }
    
    echo IDGenerator::generate();
    

    Which will give results like

    ivpa493xrx7
    d173barerui
    evpoiyjdryd
    99ej19mnau2
    

    Since there is nothing added or modified, except shuffling the bits around, there should not be any duplicated values and everything seems random. Voilà!

    ** Update (2014-02-24) **

    I update this piece of code since the time it was originally posted. You may find the revised version here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP: How to generate a random, unique, alphanumeric string? i want to
I want to generate a unique 8 character random alphanumeric string in TSQL, for
I want to auto-generate a unique 8-10 character ID string that includes a checksum
i want to generate a unique number from a table. It has to be
I've two strings StringA, StringB. I want to generate a unique string to denote
I want to generate unique number for each of my site users. How can
I want to generate alphanumeric unique numbers but the format should be like this
I want to generate unique random, N-valued key. This key can contain numbers and
I want to generate unique id's everytime i call methode generateCustumerId(). The generated id
In my .bat file I want to generate a unique name for files/directories based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.