はじめに
メール認証とかに使えそう...
コード
function random_words($limit,$is_flag = 0){
$word = null;
if($is_flag===0){
$base_string = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',0,1,2,3,4,5,6,7,8,9];
}else if($is_flag===1){
$base_string = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
}else if($is_flag===2){
$base_string = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',0,1,2,3,4,5,6,7,8,9,'*','-','_'];
}
for( $i=0; $i<$limit; $i++ ) {
$word .= $base_string[mt_rand( 0, count($base_string)-1)];
}
return $word;
}
使い方
random_words(文字数制限,フラッグ);
フラッグの種類
0
デフォルト。英語文字と数字から文字列を作成する。
1
英語文字から文字列を作成する
2
英語文字・数字・記号(*
or -
or _
)から文字列を作成s