這篇文章主要介紹了php源碼分析之DZX1.5隨機數函數random用法,實例分析了DZX1.5中random函數生成隨機字符串的使用技巧,需要的朋友可以參考下
本文實例講述了php源碼分析之DZX1.5隨機數函數random用法,分享給大家供大家參考,具體如下:
- <?php
- /**
- * @param int $length: 隨機數長度
- * @param int $numeric: 0或非0,其中0表示隨機數由全數字組成,非0表示隨機數由全字母組成
- * @return string: 返回生成的隨機數
- */
- function random($length, $numeric = 0) {
- $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
- $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
- $hash = '';
- $max = strlen($seed) - 1;
- for($i = 0; $i < $length; $i++) {
- $hash .= $seed{mt_rand(0, $max)};
- }
- return $hash;
- }
- echo random('15',1);
- echo '<br />';
- echo random('15');
- /*End of php*/
運行結果如下:
228283270677112
SjqffsU76994qfS
新聞熱點
疑難解答