Today is Sunday
September 5, 2010

Search Results Tag: php random string

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: zgtf2
10 char string: 0yd0lwjz31
15 char string: 7negzvmjx10esjy
20 char string: 3129j633hzjhhmlfv3
50 char string: w7asalucco3tlh36r5ehkxe620d940f1ndr9c8od7wuru7xttn

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