This is a simple yet highly customizable function that allows you to generate random strings using PHP. The random strings can be used for various purposes, including:
- Login Systems
- Random password for new registrations
- Random password for users requesting password reset
- Referral Codes
- Promotional Codes
- Primary Keys for tables where using integers as primary keys is not desirable
The following code example generates an 8 characters long random string that contains 1 digit:
<?php function random_string() { $character_set_array = array(); $character_set_array[] = array('count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz'); $character_set_array[] = array('count' => 1, 'characters' => '0123456789'); $temp_array = array(); foreach ($character_set_array as $character_set) { for ($i = 0; $i < $character_set['count']; $i++) { $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)]; } } shuffle($temp_array); return implode('', $temp_array); } ?>
Sample Output
ucvtbp3v xmgrxvo1 vmk1hfav jeufqvt3 maujh0og ywx4rkgs fhiay3cd i1bmbsrh
For better readability and ease of customization, I have split the $character_set_array
definition on to multiple lines. This code relies on arrays and the following PHP functions:
Customization
With minor changes to the $character_set_array
, you can customize the length and characters of the generated strings.
Complex password that contains 1 digit and 1 symbol
<?php function random_string() { $character_set_array = array(); $character_set_array[] = array('count' => 6, 'characters' => 'abcdefghijklmnopqrstuvwxyz'); $character_set_array[] = array('count' => 1, 'characters' => '0123456789'); $character_set_array[] = array('count' => 1, 'characters' => '!@#$+-*&?:'); $temp_array = array(); foreach ($character_set_array as $character_set) { for ($i = 0; $i < $character_set['count']; $i++) { $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)]; } } shuffle($temp_array); return implode('', $temp_array); } ?>
Sample Output
2sczkf-j ma1qcq-g dfqq8sf* 0n*hjfei ez-rstt5 z$ttabe2 uemlo&f6 6hb$anag
Even more complex password that contains 1 upper-case character, 1 digit and 1 symbol
<?php function random_string() { $character_set_array = array(); $character_set_array[] = array('count' => 5, 'characters' => 'abcdefghijklmnopqrstuvwxyz'); $character_set_array[] = array('count' => 1, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); $character_set_array[] = array('count' => 1, 'characters' => '0123456789'); $character_set_array[] = array('count' => 1, 'characters' => '!@#$+-*&?:'); $temp_array = array(); foreach ($character_set_array as $character_set) { for ($i = 0; $i < $character_set['count']; $i++) { $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)]; } } shuffle($temp_array); return implode('', $temp_array); } ?>
Sample Output
mczt0:Rm 2dwvJp!v wo9iVk@b c5xmx:xR a!Xwm5qk oyv!p8Sg Tiu8rb&w rmw&fa1I
Referral code containing mix of digits and upper-case characters
<?php function random_string() { $character_set_array = array(); $character_set_array[] = array('count' => 4, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); $character_set_array[] = array('count' => 4, 'characters' => '0123456789'); $temp_array = array(); foreach ($character_set_array as $character_set) { for ($i = 0; $i < $character_set['count']; $i++) { $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)]; } } shuffle($temp_array); return implode('', $temp_array); } ?>
Sample Output
EVH46H33 7248YPJP F02W4M5E 0X550PII Z64OCR86 X58W6Y0R 9JP2E06R 7S38OOJ7
If you are an ASP/VBScript programmer, you might be interested in the VBScript translation of the random string function.