Skip to content

Instantly share code, notes, and snippets.

@jgornick
Last active January 5, 2017 05:05
Show Gist options
  • Save jgornick/33294 to your computer and use it in GitHub Desktop.
Save jgornick/33294 to your computer and use it in GitHub Desktop.
PHP: Generate an 8 character long random string
<?php
///////////////////////////////////////////////////////////////////////////////
// Generate an 8 character long random string
///////////////////////////////////////////////////////////////////////////////
$chars = array('0', '1', '2', '3', '4', '5', '6', 'A', 'B', 'C', 'D', 'E', 'F');
$asset_key = '';
for ($x = 0; $x < 8; $x++)
$asset_key .= $chars[rand(0, 13)];
$asset_key = strtolower($asset_key);
print($asset_key . '<br />');
///////////////////////////////////////////////////////////////////////////////
// OR
///////////////////////////////////////////////////////////////////////////////
$asset_key = strtolower(substr(md5(uniqid(rand(), true)), 0, 8));
print($asset_key . '<br />');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment