DZX1.5隨機數函數random分享

利用base_covert生成隨機數(全數字或全字符)php

代碼文件:dom

1.  Source/function/function_core.php函數

 

function random($length, $numeric = ) {spa

    $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);orm

    $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));md5

    $hash = '';ci

    $max = strlen($seed) - 1;字符串

    for($i = ; $i < $length; $i++) {原型

       $hash .= $seed{mt_rand(, $max)};string

    }

    return $hash;

}

 

base_convert函數原型 (PHP手冊)

base_convert

(PHP 4, PHP 5)

base_convert — 在任意進制之間轉換數字

說明

string base_convert ( string $number , int $frombase , int $tobase )

返回一字符串,包含 number tobase 進制的表示。number 自己的進制由 frombase 指定。frombase tobase 都只能在 2 36 之間(包括 2 36)。高於十進制的數字用字母 a-z 表示,例如 a 表示 10b 表示 11 以及 z 表示 35

相關文章
相關標籤/搜索