Skip to content

Instantly share code, notes, and snippets.

@matstani
Last active December 29, 2017 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matstani/bca8f605d2ef679ce3c6f9f2294e5ed7 to your computer and use it in GitHub Desktop.
Save matstani/bca8f605d2ef679ce3c6f9f2294e5ed7 to your computer and use it in GitHub Desktop.
PHPで英数字混在パスワード生成
<?php
define('PASSWORD_LENGTH', 6);
function randGet($chars) {
$n = rand(0, strlen($chars) - 1);
return $chars[$n];
}
function genpwd($length) {
$alpha = 'abcdefghijklmnopqrstuwxyz';
$digit = '0123456789';
$pwd = array();
// アルファベットと数字をそれぞれ最低1文字含む
$pwd[] = randGet($alpha);
$pwd[] = randGet($digit);
$num_rest = $length - count($pwd);
for($i = 0; $i < $num_rest; $i++) {
$pwd[] = randGet($alpha . $digit);
}
shuffle($pwd);
return implode($pwd);
}
echo genpwd(PASSWORD_LENGTH)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment