LoginSignup
1
0

More than 1 year has passed since last update.

指定文字数でランダムな文字列を作成するphp関数を作った

Posted at

はじめに

メール認証とかに使えそう...

コード

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

1
0
0

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
0