程式碼如下:
/** * 隨機字元 * @param number $length 長度 * @param string $type 型別 * @param number $convert 轉換大小寫 * @return string */ function random($length=6, $type='string', $convert=0){ $config = array( 'number'=>'1234567890', 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789', 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ); if(!isset($config[$type])) $type = 'string'; $string = $config[$type]; $code = ''; $strlen = strlen($string) -1; for($i = 0; $i < $length; $i++){ $code .= $string{mt_rand(0, $strlen)}; } if(!empty($convert)){ $code = ($convert > 0)? strtoupper($code) : strtolower($code); } return $code; }
以上就是PHP最強大的隨機字串生成函數的詳細內容,更多請關注TW511.COM其它相關文章!