Skip to content

Instantly share code, notes, and snippets.

@ToshY
Last active May 6, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToshY/7ad8aff897a9c86aafc1417e39701b27 to your computer and use it in GitHub Desktop.
Save ToshY/7ad8aff897a9c86aafc1417e39701b27 to your computer and use it in GitHub Desktop.
Create a random alphanumeric string
<?php
function gRS($length, $opt = '111') {
$r = '';
//numbers only
$r .= ( ( (int) $opt[0] === 1) ? '012345678923456789' : '');
//numbers with small letters
$r .= ( ( (int) $opt[1] === 1) ? 'abcdefghijklmnopqrstuvwxyz' : '');
//numbers with small and capital letters
$r .= ( ( (int) $opt[2] === 1) ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' : '');
return ( ($r) ? substr( str_shuffle( str_repeat( $r, ceil( $length / strlen( $r ) ) ) ), 1, $length) : NULL);
}
# gRS(20);
Iio6KF7hNYbdmGUx2TB4
# gRS(20,'101');
5K4DJXW5L192REO0UFI6
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment