我有一个php函数,它检查密码以查看它们是否强大。我想将输出放入一个包含三列的表中。第一个标有"密码"第二个标有" Strong"第三个标签"测试失败"。我如何获取我所拥有的并使其成为所以foreach的每次迭代都使用发送到函数和从函数发送的信息填充表格?对不起HTML并不是我的强项。
Password Strong Tests Failed
abcd No no capital, no special char, no uppercase
这是我希望每个密码都要做的一个例子,这就是我所拥有的:
<?php
//function to test password
function validatePassword($pwd) {
//create array to store test information
$messages = [];
//test for at least 8 characters
if (strlen($pwd) < 8) {
$messages []= "too short<br />";
}
//test for max length
if (strlen($pwd) > 16) {
$messages []= "too long<br />";
}
//test to see if password contains number
if(!preg_match("#[0-9]+#", $pwd)) {
$messages []= "no number<br />";
}
//test to see if password has capital letter
if(!preg_match("#[A-Z]+#", $pwd)) {
$messages []= "no capital<br />";
}
//test to see if password has a lowercase letter
if(!preg_match("#[a-z]+#", $pwd)) {
$messages []= "no lowercase<br />";
}
//test to see if password has special character
if(!preg_match("#[^0-9A-Za-z]#", $pwd)) {
$messages []= "no special character<br />";
}
//test to see if password contains a space
if (strpos($pwd, ' ') > 0) {
$messages []= "has space(s)<br />";
}
//password passed all tests
if (empty($messages)) {
return "Password is acceptable<br />";
}
//return the array
return implode("\n", $messages);
}
//create and initialize the array to hold passwords to be tested
$Passwords = array("donkeypass", "password", "Prebyt1na!", "1234", "abcd", "narW1@asndk", "pasS w0rd!", "maK%sh1ft", "mypasswordisthebestpasswordever!23493484023", "sD123#vAr2@y7");
//loop through each element of the array
foreach ($Passwords as $value) {
//send each element to function to validate passwords and print results
echo "Password Tested: " . $value . "<br /> Results: </br>" . validatePassword($value), "<br /><br />";
}
?>
感谢您的帮助!
答案 0 :(得分:2)
当密码可以接受时,必须更改返回值,以便更容易处理表中的消息!
对于其他人,我很抱歉我使用了回声用于HTML,我使用的是网络PHP测试程序,并且它不允许嵌入HTML:(
将代码复制并粘贴到此处进行尝试 http://phptester.net/
//function to test password
function validatePassword($pwd) {
//create array to store test information
$messages = [];
//test for at least 8 characters
if (strlen($pwd) < 8) {
$messages []= "too short<br />";
}
//test for max length
if (strlen($pwd) > 16) {
$messages []= "too long<br />";
}
//test to see if password contains number
if(!preg_match("#[0-9]+#", $pwd)) {
$messages []= "no number<br />";
}
//test to see if password has capital letter
if(!preg_match("#[A-Z]+#", $pwd)) {
$messages []= "no capital<br />";
}
//test to see if password has a lowercase letter
if(!preg_match("#[a-z]+#", $pwd)) {
$messages []= "no lowercase<br />";
}
//test to see if password has special character
if(!preg_match("#[^0-9A-Za-z]#", $pwd)) {
$messages []= "no special character<br />";
}
//test to see if password contains a space
if (strpos($pwd, ' ') > 0) {
$messages []= "has space(s)<br />";
}
//password passed all tests
if (empty($messages)) {
return 1;
}
//return the array
return implode("\n", $messages);
}
//create and initialize the array to hold passwords to be tested
$Passwords = array("donkeypass", "password", "Prebyt1na!", "1234", "abcd", "narW1@asndk", "pasS w0rd!", "maK%sh1ft", "mypasswordisthebestpasswordever!23493484023", "sD123#vAr2@y7");
echo '<table>
<tr>
<td>Pass</td><td>Strong</td><td>Tests Failed</td>
</tr>';
//loop through each element of the array
foreach ($Passwords as $value) {
//send each element to function to validate passwords and print results
$strong = validatePassword($value) == 1 ? 'yes' : 'no';
$msg = validatePassword($value) != 1 ? validatePassword($value) : 'Password is Acceptable';
echo '
<tr>
<td>'.$value.'</td>
<td>'.$strong.'</td>
<td>'.$msg.'</td>
</tr>';
}
echo '
</table>';