/ Published in: PHP
I didn't make this, I just found it online and found it really useful for generating unique IDs for stuff.
Have fun using this.
Expand |
Embed | Plain Text
function genRandomString($length) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $string = ''; for ($p = 0; $p < $length; $p++) { } return $string; } ////////////////////////////////////// // Example : // // // // echo genRandomString(rand(3,7)); // // // // Could Print Out: Ac1dF // //////////////////////////////////////
Comments
Subscribe to comments
You need to login to post a comment.

There is no reason to use a loop. Just shuffle the string and take out the segment you need.
function str_rand($len=10) { $shuffled = str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); return substr($shuffled, 0, $len); }I have tested this code to generate 404 32-chars long strings. Lengths of the strings are between 30 and 33 chars. Code that Sverri posted works like a charm! I got all unique strings and they were all 32 chars long.