Function of acquiring a random string

IF (! function_exists ( 'Random' )) 
{ 
    / * * 
      * Get random string 
      * @param number $ length length 
      * @param string $ type Type 
      * @param number $ convert change case 
      * @return string random string 
      * / 
    function Random ( $ length =. 6, $ type = 'String', $ Convert = -1 ) 
    { 
        $ 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;
    }
}

 

Guess you like

Origin www.cnblogs.com/songkaixin/p/11122174.html