ホームページ >バックエンド開発 >PHPチュートリアル >PHP_PHP チュートリアルで乱数を取得した経験
関数:
ランダムな文字列を取得
乱数を取得するためのPHPパラメータ:
1, (int)$length = 32 #ランダムな文字の長さ
2, (int)$モード = 0 #ランダムな文字タイプ、0 は大文字と小文字、数字、1 は数字、2 は小文字、3 は大文字、4 は大文字と小文字、5 は大文字と数字、6 は小文字、数値
PHP は乱数定義クラスコードを取得します
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>class getRandstrClass </span></li><li class="alt"><span>{ </span></li><li><span>function getCode ($</span><span class="attribute">length</span><span> = </span><span class="attribute-value">32</span><span>, $</span><span class="attribute">mode</span><span> = </span><span class="attribute-value">0</span><span>) </span></li><li class="alt"><span>{ </span></li><li><span>switch ($mode) { </span></li><li class="alt"><span>case '1': </span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'1234567890'</span><span>; </span></li><li class="alt"><span>break; </span></li><li><span>case '2': </span></li><li class="alt"><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'abcdefghijklmnopqrstuvwxyz'</span><span>; </span></li><li><span>break; </span></li><li class="alt"><span>case '3': </span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span>; </span></li><li class="alt"><span>break; </span></li><li><span>case '4': </span></li><li class="alt"><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZa<br />bcdefghijklmnopqrstuvwxyz'</span><span>;break; </span></li><li><span>case '5': </span></li><li class="alt"><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXY<br />Z1234567890'</span><span>; </span></li><li><span>break; </span></li><li class="alt"><span>case '6': </span></li><li><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'abcdefghijklmnopqrstuvwxyz<br />1234567890'</span><span>; </span></li><li class="alt"><span>break; </span></li><li><span>default: </span></li><li class="alt"><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZa<br />bcdefghijklmnopqrstuvwxyz1234567890'</span><span>; </span></li><li><span>break; </span></li><li class="alt"><span>} </span></li><li><span>$</span><span class="attribute">randString</span><span> = </span><span class="attribute-value">''</span><span>; </span></li><li class="alt"><span>$</span><span class="attribute">len</span><span> = </span><span class="attribute-value">strlen</span><span>($str)-1; </span></li><li><span>for($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>;$i </span><span class="tag"><</span><span> $length;$i ++){ </span></li><li class="alt"><span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">mt_rand</span><span>(0, $len); </span></li><li><span>$randString </span><span class="attribute">.</span><span>= $str[$num]; </span></li><li class="alt"><span>} </span></li><li><span>return $randString ; </span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
PHP は乱数の使用手順を取得します:
1. 上記のコードをrandom.phpとして保存します
2. 必要なページにrandom.phpを導入します
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>require_once('random.php'); </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
3. 先ほど定義したクラス
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">code</span><span> = </span><span class="attribute-value">new</span><span> getRandstrClass(); </span></li><li class="alt"><span>$</span><span class="attribute">length</span><span> = </span><span class="attribute-value">32</span><span>; </span></li><li><span>$</span><span class="attribute">mode</span><span> = </span><span class="attribute-value">0</span><span>; </span></li><li class="alt"><span>$</span><span class="attribute">str</span><span> = $code-</span><span class="tag">></span><span>getCode($length, $mode); </span></span></li> <li><span>echo $str; </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
を使用します。 以上がPHPで乱数を取得するための応用メソッドです。