Hot Koehls
  • Email
  • Feedburner
  • Linkedin
  • Twitter
  • Home
  • About
  • Archives
  • Contact
  • Software
    • S3imple Backup
    • Twitter Feed Archiver
    • FileTime
    • Flickr API Demo
Search
Home» For techies » Versatile random string generator

Versatile random string generator

Posted by Frank - December 18, 2008 - For techies
4

A cursory glance around the web will reveal a ton of PHP-based random string generators. With enough looking you’ll find generators that do any of the following:

  • Strings with letters
  • Strings with numbers
  • Strings with letters and numbers
  • Uppercase, lowercase
  • Fixed, variable length strings
  • Option to include symbols

Problem is, none of them ever incorporated all this functionality. Every generator was a hodgepodge, e.g. some forced inclusion of numbers, or allowed either upper or lowercase, not both. All of these are great options, and it would be great to have all of them at your disposal in one tight function.

No more! I got so sick of finding shortcomings that I finally just put it all together myself. The following function allows you to choose a string length, as well as the character sets to use when building your random string. You can even include a set more than once, giving greater usage weight to certain characters. Finally, complete flexibility!

function koehl_generator($length = 10, $charsets = 'lower') {
  $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $lower = 'abcdefghijklmnopqrstuvwxyz';
  $numbers = '1234567890';
  $symbols = '!@#$%^&*()-_=+<>,.?/:;[]{}|~';

  if (!is_array($charsets)) $charsets = array($charsets);
  $charset_pool = array();
  foreach ($charsets as $set) {
    $charset_pool[] = $$set;
  }
  $max = (sizeof($charset_pool) - 1);
  $v = '';
  for ($i = 0; $i < $length; $i++) {
    $this_pool = $charset_pool[rand(0, $max)];
    $v .= $this_pool[(rand() % strlen($this_pool))];
  }
  return $v;
}

// usage examples
echo koehl_generator(10, 'upper') . '';
echo koehl_generator(10, 'lower') . '';
echo koehl_generator(10, 'numbers') . '';
echo koehl_generator(10, 'symbols') . '';
echo koehl_generator(10, array('upper', 'lower')) . '';
// order of the array in the second argument does not matter
echo koehl_generator(15, array('lower', 'upper', 'numbers')) . '';
echo koehl_generator(15, array('lower', 'numbers', 'upper')) . '';
echo koehl_generator(15, array('upper', 'lower', 'numbers', 'symbols')) . '';
echo koehl_generator(15, array('lower', 'symbols')) . '';
// note how often letters appear in the next one
echo koehl_generator(20, array('lower', 'lower', 'numbers')) . '';

Adding your own custom set is easy too. Include a new set following the syntax of the existing ones, then call your new set by variable name in the second argument...

// add this to the top of the function...
$the_basics = 'abc123';

// then use it like so...
echo koehl_generator(5, 'the_basics') . '';
echo koehl_generator(10, array('the_basics', 'symbols')) . '';

If you find this useful, please be sure to give me some link love, just a reference URL to this page in your code would be fine. Using the share buttons below would be great as well!

extension, programming, security

4 comments on “Versatile random string generator”

  1. Chris says:
    December 18, 2008 at 11:47 pm

    Finally found a use for $$. :)

  2. Frank says:
    December 19, 2008 at 10:57 am

    The variable variable — aka the “double-dollar” or “dollar-dollar” — is such a wicked sleeper ability in PHP. God help all us OSS coders if that gets popular.

  3. Ethan says:
    December 20, 2008 at 9:26 am

    Chris, I second that! I had long been trying to wrap my head around a practical use for the variable-variable. While I understood the logic, I have not had an opportunity to apply it.

    This was great…. Thanks Frank!

  4. Frank says:
    December 24, 2008 at 2:26 pm

    You’re welcome, Ethan!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

  • For entrepreneurs
  • For everyone
  • For techies

Latest Tweets

  • The word traps planners plan themselves into | Life. Then strategy http://t.co/iANAdASb
    May 8, 2012 - 2:43 pm
  • Random network security tip for those about to appear on TV - Boing Boing http://t.co/tC1lXFQ4
    May 8, 2012 - 1:42 pm
  • A Picture http://t.co/H846Uy69
    April 27, 2012 - 12:25 pm
  • The Broken "Buy-One, Give-One" Model: 3 Ways to Save Toms Shoes | Co.Exist: World changing ideas and innovation http://t.co/RI0sVMW6
    April 10, 2012 - 12:23 pm

Recent Comments

  • whiz on What 255 characters looks like
  • Andrew on Find the second (or third, or fourth) occurence in a string
  • IanArcher on Get number of message parts in an email using PHP
  • Usama on Remove parent directories from tar archives
  • Frank on It’s dangerous to go alone

Recent Posts

  • It’s dangerous to go alone
  • Create Self-Signed Wildcard SSL Certificate
  • What comes after the yottabyte?
  • Write code like they do in Hollywood
  • Brian Rolle machine gun celebration
(c) 2012 Frank Koehl. All Rights Reserved.
  • Contact Us
  • Sitemap