note 40513 modified in function.array-rand by vrana

From: Date: Tue, 17 Aug 2004 14:48:35 +0000
Subject: note 40513 modified in function.array-rand by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80096@lists.php.net to get a copy of this message
Just thought I would contribute a password generation function that uses array_rand. I wrote this because I could not find anywhere a PHP equivalent of the ability that the String::Random module in Perl has, which allows you to specify a schema for how you want the random string created. In other words: I want 2 Uppercase, 3 lowercase, 2 intergers...etc. This isn't too comprehensive, notably it doesn't account for one choosing more itterations of a particular type than there are in the array (ie - choosing more than 10 numbers from output). Additionally, this doesn't allow for any repeated characters. Hope it can be of use... comments appreciated. <?php function &doGeneratePasswords() { ////////////////////////////////////// // lowercase L left out for clarity // ////////////////////////////////////// $l_achLowercase = array("a","b","c","d","e","f","g","h", "i","j","k","m","n","o","p","q", "r","s","t","u","v","w","x", "y","z"); $l_iNumLowercase = count($l_achLowercase); //////////////////////////////////////////// // uppercase I and O left out for clarity // //////////////////////////////////////////// $l_achUppercase = array("A","B","C","D","E","F","G","H", "J","K","L","M","N","P","Q", "R","S","T","U","V","W", "X","Y","Z"); $l_iNumUppercase = count($l_achUppercase); $l_aiNumbers = array("1","2","3","4","5","6","7","8","9","0"); $l_iNumNumbers = count($l_aiNumbers); $l_achSpecialChars = array("!","#","%","@","*","&"); $l_iNumSpecialChars = count($l_achSpecialChars); /////////////////////////////////////////////////////////////////// // Make sure to create enough blank spaces as you want passwords // /////////////////////////////////////////////////////////////////// $l_astPasswds = array("","","",""); ////////////////////////////////////////// // Hopefully these are self explanatory // ////////////////////////////////////////// $l_astPasswdSchemes = array("SLUUSLNN","LSUNLLNU","NNUSLLSN","LNLSUNLU"); $l_iNumPasswds = count($l_astPasswdSchemes); for ($i=0; $i < $l_iNumPasswds; $i++) { $l_iSchemeLength = strlen($l_astPasswdSchemes[$i]); $l_achRandLowercase = array_values(array_rand($l_achLowercase, $l_iNumLowercase)); $l_achRandUppercase = array_values(array_rand($l_achUppercase, $l_iNumUppercase)); $l_aiRandNumbers = array_values(array_rand($l_aiNumbers, $l_iNumNumbers)); $l_achRandSpecialChars = array_values(array_rand($l_achSpecialChars, $l_iNumSpecialChars)); for ($j=0; $j < $l_iSchemeLength; $j++) { $l_chCurrentOne = $l_astPasswdSchemes[$i]{$j}; switch ($l_chCurrentOne) { case "L": $l_astPasswds[$i] .= $l_achLowercase[array_shift($l_achRandLowercase)]; break; case "U": $l_astPasswds[$i] .= $l_achUppercase[array_shift($l_achRandUppercase)]; break; case "N": $l_astPasswds[$i] .= $l_aiNumbers[array_shift($l_aiRandNumbers)]; break; case "S": $l_astPasswds[$i] .= $l_achSpecialChars[array_shift($l_achRandSpecialChars)]; break; default: break; } } } return $l_astPasswds; } ?> --was-- Just thought I would contribute a password generation function that uses array_rand. I wrote this because I could not find anywhere a PHP equivalent of the ability that the String::Random module in Perl has, which allows you to specify a schema for how you want the random string created. In other words: I want 2 Uppercase, 3 lowercase, 2 intergers...etc. This isn't too comprehensive, notably it doesn't account for one choosing more itterations of a particular type than there are in the array (ie - choosing more than 10 numbers from output). Additionally, this doesn't allow for any repeated characters. Hope it can be of use... comments appreciated. function &doGeneratePasswords() { ////////////////////////////////////// // lowercase L left out for clarity // ////////////////////////////////////// $l_achLowercase = array("a","b","c","d","e","f","g","h", "i","j","k","m","n","o","p","q", "r","s","t","u","v","w","x", "y","z"); $l_iNumLowercase = count($l_achLowercase); //////////////////////////////////////////// // uppercase I and O left out for clarity // //////////////////////////////////////////// $l_achUppercase = array("A","B","C","D","E","F","G","H", "J","K","L","M","N","P","Q", "R","S","T","U","V","W", "X","Y","Z"); $l_iNumUppercase = count($l_achUppercase); $l_aiNumbers = array("1","2","3","4","5","6","7","8","9","0"); $l_iNumNumbers = count($l_aiNumbers); $l_achSpecialChars = array("!","#","%","@","*","&"); $l_iNumSpecialChars = count($l_achSpecialChars); /////////////////////////////////////////////////////////////////// // Make sure to create enough blank spaces as you want passwords // /////////////////////////////////////////////////////////////////// $l_astPasswds = array("","","",""); ////////////////////////////////////////// // Hopefully these are self explanatory // ////////////////////////////////////////// $l_astPasswdSchemes = array("SLUUSLNN","LSUNLLNU","NNUSLLSN","LNLSUNLU"); $l_iNumPasswds = count($l_astPasswdSchemes); for ($i=0; $i < $l_iNumPasswds; $i++) { $l_iSchemeLength = strlen($l_astPasswdSchemes[$i]); $l_achRandLowercase = array_values(array_rand($l_achLowercase, $l_iNumLowercase)); $l_achRandUppercase = array_values(array_rand($l_achUppercase, $l_iNumUppercase)); $l_aiRandNumbers = array_values(array_rand($l_aiNumbers, $l_iNumNumbers)); $l_achRandSpecialChars = array_values(array_rand($l_achSpecialChars, $l_iNumSpecialChars)); for ($j=0; $j < $l_iSchemeLength; $j++) { $l_chCurrentOne = $l_astPasswdSchemes[$i]{$j}; switch ($l_chCurrentOne) { case "L": $l_astPasswds[$i] .= $l_achLowercase[array_shift($l_achRandLowercase)]; break; case "U": $l_astPasswds[$i] .= $l_achUppercase[array_shift($l_achRandUppercase)]; break; case "N": $l_astPasswds[$i] .= $l_aiNumbers[array_shift($l_aiRandNumbers)]; break; case "S": $l_astPasswds[$i] .= $l_achSpecialChars[array_shift($l_achRandSpecialChars)]; break; default: break; } } } return $l_astPasswds; } http://php.net/manual/en/function.array-rand.php

« previous php.notes (#80096) next »