Home  >  Article  >  Backend Development  >  PHP development skills (4) - Detailed explanation of two implementation methods for obtaining random strings

PHP development skills (4) - Detailed explanation of two implementation methods for obtaining random strings

黄舟
黄舟Original
2017-03-10 18:42:111229browse

There are many ways to obtain random strings. As long as your algorithm is well designed, you can get the results you want easily and quickly. Obtaining random strings is also used a lot in our daily work programming, such as: the verification code to be entered when logging in, the verification code for sending mobile phone SMS verification, and in order to prevent duplication of order numbers when we generate orders, we will Add a random string after the inherent odd number to increase the guarantee that the number will not be repeated, etc. In fact, anyone who knows programming knows that there is no real randomness. Everything can find its rules. It is just a question of whether it is difficult or not. I won’t say more about this.

I have studied and written two methods of generating random strings here, each with its own advantages and disadvantages. The main purpose is to learn and make progress together with everyone. If you have good opinions and suggestions, please leave a message. I won’t go into details below. The code is relatively simple. The main thing is to provide an idea or method. You can read the code directly

 '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;  
}  
  
/** 
 * 方法二:获取随机字符串 
 * @param int $randLength 长度 
 * @param int $addtime 是否加入当前时间戳 
 * @param int $includenumber 是否包含数字 
 * @return string 
 */  
function rand_str($randLength = 6, $addtime = 1, $includenumber = 0)  
{  
    if ($includenumber) {  
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';  
    } else {  
        $chars = 'abcdefghijklmnopqrstuvwxyz';  
    }  
    $len = strlen($chars);  
    $randStr = '';  
    for ($i = 0; $i < $randLength; $i++) {  
        $randStr .= $chars[rand(0, $len - 1)];  
    }  
    $tokenvalue = $randStr;  
    if ($addtime) {  
        $tokenvalue = $randStr . time();  
    }  
    return $tokenvalue;  
}  
  
//其中也可以放多个参数  
//random(4,'number');  
//random(6,'letter',1);  
echo random(10);  
//方法二同样  
echo rand_str(6);  
  
?>


The above is the detailed content of PHP development skills (4) - Detailed explanation of two implementation methods for obtaining random strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn