我试图在PHP中创建一个随机字符串,我得到绝对没有输出:
<?php
function RandomString()
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randstring = '';
for ($i = 0; $i < 10; $i++) {
$randstring = $characters[rand(0, strlen($characters))];
}
return $randstring;
}
RandomString();
echo $randstring;
我做错了什么?
一个完整的解决方案(课程加测试),部分基于上面的一些建议…
class TokenFactory
{
private const LENGTH = 12;
private const ALLOWED = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ~!@#$%^&*{}';
private const MIN_NUMBER_OF_DIGITS = 1;
private const MIN_NUMBER_OF_CAPS = 1;
private const MIN_NUMBER_OF_SPECIALS = 1;
private const MIN_NUMBER_OF_LETTERS = 1;
/**
* @return string
* @throws \Exception
*/
public function make(): string
{
$pass = $this->generateToken();
if ($this->isTokenValid($pass)) {
return $pass;
} else {
return $this->make();
}
}
/**
* @return string
* @throws \Exception
*/
private function generateToken(): string
{
$allowedCharacters = self::ALLOWED;
$token = '';
$max = mb_strlen($allowedCharacters, '8bit') - 1;
for ($i = 0; $i < self::LENGTH; ++$i) {
$token .= $allowedCharacters[random_int(0, $max)];
}
return $token;
}
/**
* @param $token
* @return bool
*/
private function isTokenValid($token): bool
{
$numberOfDigits = preg_match_all("/[0-9]/", $token);
$numberOfCaps = preg_match_all("/[A-Z]/", $token);
$numberOfSpecials = preg_match_all("/[~!@#\$%^&*{}]/", $token);
$numberOfLetters = preg_match_all("/[a-z]/", $token);
return
$numberOfDigits > self::MIN_NUMBER_OF_DIGITS
&& $numberOfCaps > self::MIN_NUMBER_OF_CAPS
&& $numberOfSpecials > self::MIN_NUMBER_OF_SPECIALS
&& $numberOfLetters > self::MIN_NUMBER_OF_LETTERS
;
}
}
class TokenFactoryTest
{
public function test_correct_syntax()
{
/**
* Arrange
*/
$length = 12;
$numberOfChecks = 1000;
/**
* Act & Assert
*/
$class = new TokenFactory();
$i = 0;
while ($i < $numberOfChecks) {
$generatedToken = $class->make();
$numberOfDigits = preg_match_all( "/[0-9]/", $generatedToken );
$numberOfCaps = preg_match_all( "/[A-Z]/", $generatedToken );
$numberOfSpecials = preg_match_all("/[~!@#\$%^&*{}]/", $generatedToken);
$numberOfLetters = preg_match_all("/[a-z]/", $generatedToken);
Assert::assertEquals($length, strlen($generatedToken));
Assert::assertTrue($numberOfDigits >= 1, 'Digit error: ' . $generatedToken);
Assert::assertTrue($numberOfCaps >= 1, 'Caps error: ' . $generatedToken);
Assert::assertTrue($numberOfSpecials >= 1, 'Specials error: ' . $generatedToken);
Assert::assertTrue($numberOfLetters >= 1, 'Letters error: ' . $generatedToken);
$i++;
}
}
}
顺便说一句,请确保在适合您需要的地方捕获该异常!
递归解决方案:
public static function _random(string $set , int $length): string
{
$setLength = strlen($set);
$randomKey = random_int(0, $setLength - 1);
$firstPiece = substr($set, 0, $randomKey);
$secondPiece = substr($set, $randomKey, $setLength - $randomKey);
$removedCharacter = $firstPiece[strlen($firstPiece) - 1] ?? null;
if(null === $removedCharacter || $length === 0) {
return '';
}
$firstPieceWithoutTheLastChar = substr($firstPiece, 0, -1);
return $removedCharacter . self::_random($firstPieceWithoutTheLastChar . $secondPiece, $length - 1);
}
不错的表现,https://3v4l.org/aXaJ6/perf
我总是喜欢使用base64来生成随机密码或其他随机(可打印的)字符串。base64的使用确保了大量的可打印字符可用。
在shell上,我通常这样做:
base64 < /dev/urandom |head -c10
在PHP中也可以做类似的事情。然而,直接从/dev/urandom读取可能会被open_basedir限制所禁止。这就是我得出的结论:
base64_encode(
join(
'',
array_map(
function($x){ return chr(mt_rand(1,255));},
range(1,15)
)
)
);
为了得到一个真正随机的字符串,我们也需要随机输入。这就是join/array_map所做的。使用uniqid之类的东西是不够的,因为它总是有一个类似的前缀,因为它基本上是一个美化的时间戳。
如果安装了openssl扩展,当然可以使用openssl_random_pseudo_bytes(),这样会更好。
下面的函数生成任意长度的伪字符串。
/**
* Returns random string of a given length.
*/
function get_random_string($length) {
$pull = [];
while (count($pull) < $length) {
$pull = array_merge($pull, range(0, 9), range('a', 'z'), range('A', 'Z'));
}
shuffle($pull);
return substr(implode($pull), 0, $length);
}