Skip to content

Instantly share code, notes, and snippets.

@justinkelly
Created January 20, 2012 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justinkelly/1647016 to your computer and use it in GitHub Desktop.
Save justinkelly/1647016 to your computer and use it in GitHub Desktop.
Simple PHP function to generate a random apha-numeric code with only readable characters
<?php
/*
* Simple PHP function to generate a random apha-numeric code with only readable characters
*/
function randomCode($length=4) {
/* Only select from letters and numbers that are readable - no 0 or O etc..*/
$characters = "23456789ABCDEFHJKLMNPRTVWXYZ";
for ($p = 0; $p < $length; $p++)
{
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $string;
}
?>
Copy link

ghost commented Feb 16, 2016

I've forked your little function and added a possibility to get an array back with a desired amount of unique random codes. You find it here: https://gist.github.com/NoTeefy/fb830a3b385b2eb3bc58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment