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 6245065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:26:28+00:00 2026-05-24T12:26:28+00:00

So I was wonder what are some good/preferred methods for generating a ‘hex-like’ value

  • 0

So I was wonder what are some good/preferred methods for generating a ‘hex-like’ value in PHP? Preferably, I would want to restrict it to 5 characters long like such: 1e1f7

Currently this is what I am doing:

echo dechex(mt_rand(10000, 99999));

however this gives me values anywhere from 4-5 characters long, and I want to keep it at a consistent 4 or 5.

What are some ways to better generate something like this in PHP? Is there even a built in function?

Note: When I say ‘hex-like’ I really just mean a random combination of letters and numbers. There does not have to be a restriction on available letters.

  • 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-24T12:26:28+00:00Added an answer on May 24, 2026 at 12:26 pm

    Something simple like:

    $length = 5;
    $string = "";
    while ($length > 0) {
        $string .= dechex(mt_rand(0,15));
        $length -= 1;
    }
    return $string;
    

    (untested)

    Or fix your mt_rand range to: mt_rand(65535, 1048575) (10000-fffff in hex) or if you like tinfoil hats: mt_rand(hexdec("10000"), hexdec("ffffff"))

    The advantage of the while-loop approach is that it works for arbitrarily long strings. If you’d want 32 random characters you’re well over the integer limit and a single mt_rand will not work.

    If you really just want random stuff, I’d propose:

    $length = 5;
    $string = "";
    $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=+!@#$%^&*()[]"; // change to whatever characters you want
    while ($length > 0) {
        $string .= $characters[mt_rand(0,strlen($characters)-1)];
        $length -= 1;
    }
    return $string;
    

    (untested)

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

Sidebar

Related Questions

I wonder what would happen when I create a joinable thread for writing some
I got some problems with finding a bug in php/mysql application, and I wonder
I wonder in which cases it would be good to make an NSManagedObjectModel completely
I would like to store some Application-Related Metadata for Files, and NTFS Alternate Data
I wonder if there's some way of doing this, or even if it should
I wonder if someone can help shed some light on this: I drop a
I wonder if C# (or the underlying .NET framework) supports some kind of generic
hi i wonder if there are some GUI softwares to administrate a svn repo?
hi i wonder if there are some GUI softwares to administrate a svn repo?
I wonder is there any positive effect in using UNSIGNED flag on defining some

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.