A random string generator is useful for all php projects. You may use it to create tokens or to create new random password.
The generateString function generates a new random string $length characters long.
The following script generates 20 random strings whose length is 1 t 20.
<?php for ($x = 0; $x <= 21; $x++) { echo generateString($x); echo "\n"; } function generateString($length) { echo $length."\t"; if($length<=0 || $length>20) { $randomString = "------------------"; } else { $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length); } return($randomString); } ?>
Try running it
/var/www# php RandomStringGenerator.php
0 ------------------
1 9
2 f9
3 nTt
4 EieK
5 i2lQC
6 DH4dpt
7 IiGuD09
8 2fBJe3ML
9 m2XK6Q4n5
10 8emRLhJo6S
11 NbQoirYh3CM
12 0sHnvuUTCPAw
13 3vftQTzdaV5h6
14 xWzvlA0ZJnOtB4
15 n6F30hKeQdUNo1Y
16 kxzXHyMIPwAhRs6n
17 2hklu16esQU7gF0oR
18 vA7wp68tVT0l12niD5
19 KPyE0eFXIsnfUOZQo8N
20 yTGv6WMuqgoXiSeBKj2F
21 ------------------
Gg1