Skip to content

Instantly share code, notes, and snippets.

@xexu
Created February 17, 2012 18:06
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 xexu/1854667 to your computer and use it in GitHub Desktop.
Save xexu/1854667 to your computer and use it in GitHub Desktop.
Function to generate random strings of a given length containing characters from a given set
<?php
function randomStringFromCharacterSet($length, $possibleCharacters){
return substr(str_shuffle(str_repeat($possibleCharacters,$length)),0,$length);
}
//Example of usage
$possibleCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$length = 5;
for($i=0;$i<10;$i++){
echo randomStringFromCharacterSet($length, $possibleCharacters).'</br>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment