将php数组结果输出到HTML表中


Outputting php array results into a HTML table

我有一个php函数,我检查密码,看看他们是否强或不。我想把输出放到一个有三列的表中。第一个标记为"密码",第二个标记为"强",第三个标记为"测试失败"。我如何采取我所拥有的,并使其使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 />";
}
?>

谢谢你的帮助!

必须在密码可接受时更改返回值,以便更容易处理表中的消息!

对于其余的,我很抱歉我使用echo的HTML,我使用的是一个web 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>';

不要内爆消息数组。而是将结果(Yes, No等)作为最后一个元素放在其中。然后可以使用array_pop()检索它。要获取HTML表,请执行如下操作:

echo "<table>";
foreach($Password as $value){
    $out = validatePassword($value);
    $last = array_pop($out);
    echo "<tr><td>$value</td><td>$last</td><td>" . implode(",", $out) . "</td></tr>";
}
echo "</table>";