1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで半角英数字がそれぞれ必ず入るパスワードを自動生成したい!!

Posted at

自分のメモ用に置いときます。

ほぼこの記事のコピペです。

function generate_password($length = 8) {
    $pwd = array();

    // パスワード生成素材
    $pwd_material = array(
        'lower'		=> range('a', 'z'),
        'upper'		=> range('A', 'Z'),
        'number'	=> range('0', '9')
    );

    // $length分回す
    while (count($pwd) < $length) {
        // 必ず半角大文字英数字を1回は入れる
        if (count($pwd) < 3) {
            $key = key($pwd_material);
            next($pwd_material);
        }
        else {
            $key = array_rand($pwd_material);
        }
        $pwd[] = $pwd_material[$key][array_rand($pwd_material[$key])];
    }

    shuffle($pwd);
    return implode($pwd);
}
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?