我写了这个小脚本,它生成一个很好的随机密码。我遇到的问题是,当您打开页面并选择全部+将文本复制到剪贴板时。即使我使用装饰,也总有一个空间。无论如何要解决这个问题吗?
使用Firefox进行测试。
更新:在使用jsfiddle示例进行测试后,这与PHP无关,因为我在这里遇到了同样的问题。这有同样的问题,即总是在最后添加一个空格。
<?php
function random_readable_pwd($length=10){
// the wordlist from which the password gets generated
// (change them as you like)
$words = 'AbbyMallard,AbigailGabble,AbisMal,Abu,Adella,TheAgent,AgentWendyPleakley,Zini';
// Split by ",":
$words = explode(',', $words);
if (count($words) == 0){ die('Wordlist is empty!'); }
// Add words while password is smaller than the given length
$pwd = '';
while (strlen($pwd) < $length){
$r = mt_rand(0, count($words)-1);
$pwd .= $words[$r];
}
$num = mt_rand(1, 99);
if ($length > 2){
$pwd = substr($pwd,0,$length-strlen($num)).$num;
} else {
$pwd = substr($pwd, 0, $length);
}
$pass_length = strlen($pwd);
$random_position = rand(0,$pass_length);
$syms = "!@#%^*()-?";
$int = rand(0,9);
$rand_char = $syms[$int];
$pwd = substr_replace($pwd, $rand_char, $random_position, 0);
return $pwd;
}
?>
<html><head><title>Password generator</title></head>
<body><?php echo random_readable_pwd(10); ?></body>
</html>
答案 0 :(得分:2)
服务器端是什么并不重要。最后,你得到了这个:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>foo</body>
</html>
(我添加了DOCTYPE以避免含糊不清。)如果您通过Firefox检查HTML 解释(您需要使用Firebug),您会发现:
<body>foo </body>
因此需要生成不同的HTML标记。一种可能性:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
</head>
<body>
<p>foo</p>
</body>
</html>