Random password generator in PHP not returning pas

2020-06-28 16:35发布

I am trying to set up a random password generator function in php but this is not working for me. I get the error:

Notice: Array to string conversion in C:\xampp\htdocs\php-testing\index.php on line 12

Array

What am I doing wrong?

<?php
function pwGenerator($len = 10) {
    $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
    $pw = array();
    $length = strlen($charRange);
    for ($x = 0; $x < $len; $x++) {
        $n = rand(0, $length);
        $pw[] = $charRange[$n];
    }

    return $pw;
}

echo pwGenerator();

标签: php arrays
7条回答
乱世女痞
2楼-- · 2020-06-28 17:08

It can be an Easy Solution.

$arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 
'W', 'Y', '@', '#', '$', '*', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 
'd', 'e', 'f', 'g', 'h', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'w', 'y', 'z');

$arr2 = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'L', 'M', 'N', 
'P', 'Q', 'R', 'T', 'W', 'Y', 'a', 'b', 'd', 'e', 'f', 'g', 'h', 'm', 
'n', 'p', 'q', 'r', 't', 'u', 'w', 'y', 'z');

        $rnd = array_random($arr, 4);
        $rnd2 = array_random($arr2, 2);

        $password = $rnd2[0].$rnd[0].$rnd[1].$rnd[2].$rnd[3].$rnd2[1];

The First and the Last character will be only Letter. Middle characters will be Letters, Numbers or Special Characters.

查看更多
登录 后发表回答