Today is Sunday
September 5, 2010

June 23, 2010

PHP create a random string of Alpha/Numeric Characters

by Chris — Categories: Coding, PHP — Tags: , , No Comments

Have a need for a quick random string of any given length but want to limit it to a-z 0-9. Then this would be ideal for you, I really can’t think of any real world use for this. Well no thats wrong, you can use it as a random password generator. Or something to the effect of that.

<?php
function genRandomString($howbig) {
$length = $howbig;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';

for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}

return $string;
}
echo "<strong>5 char string:</strong> ". genRandomString('5') ."<br />";
echo "<strong>10 char string:</strong> ".genRandomString('10')."<br />";
echo "<strong>15 char string:</strong> ".genRandomString('15')."<br />";
echo "<strong>20 char string:</strong> ".genRandomString('20')."<br />";
echo "<strong>50 char string:</strong> ".genRandomString('50')."<br />";
?>

Example of use:
5 char string: ua4rh
10 char string: 9w2uykpv
15 char string: vcokopgr3bxxp8
20 char string: 2bobiq13jatsp3rhrzxz
50 char string: xsk9bts7w63fq3yejpfpp43hwujaqezvizmvev05er43eu6u7

No Comments »

No comments yet.

Leave a Comment

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

© 2010 Monkey Tooth Productions All rights reserved - Wallow theme by TwoBeers Crew - Powered by WordPress - Have fun!