私はあなたがhomeworkタグを探していると思う:GeneratePassword(7,'abczxc')
は、以下のいずれかの出力を返すことができます。
他の人を援助する精神で、私はコメントした解決策を投稿します。しかし、より良くなる唯一の方法は、に最初にお問い合わせください。つまり、あなたがどこに間違って行ったのか他の人に尋ねてみてください。
例/ Demo:
/**
* Generate a password N characters long consisting of characters
*
* @param int $size
* @param string $characters
* @param callback $random (optional) source of random, a function with two parameters, from and to
* @return string|NULL password
*/
function generate_password($size, $characters, $random = 'rand') {
// validate $size input
$size = (int) $size;
if ($size <= 0) {
trigger_error(sprintf('Can not create a password of size %d. [%s]', $size, __FUNCTION__), E_USER_WARNING);
return NULL;
}
if ($size > 255) {
trigger_error(sprintf('Refused to create a password of size %d as this is larger than 255. [%s]', $size, __FUNCTION__), E_USER_WARNING);
return NULL;
}
// normalize $characters input, remove duplicate characters
$characters = count_chars($characters, 3);
// validate number of characters
$length = strlen($characters);
if ($length < 1) {
trigger_error(sprintf('Can not create a random password out of %d character(s). [%s]', $length, __FUNCTION__), E_USER_WARNING);
return NULL;
}
// initialize the password result
$password = str_repeat("\x00", $size);
// get the number of characters minus one
// your string of characters actually begins at 0 and ends on the
// string-length - 1:
// $characters[0] = 'a'
// $characters[1] = 'b'
// $characters[2] = 'c'
$length--;
// get one random character per each place in the password
while ($size--)
{
// generate a random number between 0 and $length (including)
$randomValue = $random(0, $length);
// that random number is used to turn the number into a character
$character = $characters[$randomValue];
// set the random character
$password[$size] = $character;
}
// return the result
return $password;
}
そして、それは実際に投票アップを授与し、ブラッドの答えを受け入れるためにチェックマークをクリックするアスカーのは本当にいいだろう... – Crontab