Home > Backend Development > PHP Tutorial > 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

黄舟
Release: 2023-03-06 13:42:01
Original
1401 people have browsed it

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

<?php  
  
/** 
 * ======================================= 
 * Created by Zhihua_W. 
 * Author: Zhihua_W 
 * Date: 2016/12/1 0004 
 * Time: 下午 5:15 
 * Project: PHP开发小技巧 
 * Power: 两种方法实现获取随机字符串 
 * ======================================= 
 */  
  
/** 
 * 方法一:获取随机字符串 
 * @param number $length 长度 
 * @param string $type 类型 
 * @param number $convert 转换大小写 
 * @return string 随机字符串 
 */  
function random($length = 6, $type = &#39;string&#39;, $convert = 0)  
{  
    $config = array(  
        &#39;number&#39; => &#39;1234567890&#39;,  
        &#39;letter&#39; => &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;,  
        &#39;string&#39; => &#39;abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789&#39;,  
        &#39;all&#39; => &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890&#39;  
    );  
  
    if (!isset($config[$type]))  
        $type = &#39;string&#39;;  
    $string = $config[$type];  
  
    $code = &#39;&#39;;  
    $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 = &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789&#39;;  
    } else {  
        $chars = &#39;abcdefghijklmnopqrstuvwxyz&#39;;  
    }  
    $len = strlen($chars);  
    $randStr = &#39;&#39;;  
    for ($i = 0; $i < $randLength; $i++) {  
        $randStr .= $chars[rand(0, $len - 1)];  
    }  
    $tokenvalue = $randStr;  
    if ($addtime) {  
        $tokenvalue = $randStr . time();  
    }  
    return $tokenvalue;  
}  
  
//其中也可以放多个参数  
//random(4,&#39;number&#39;);  
//random(6,&#39;letter&#39;,1);  
echo random(10);  
//方法二同样  
echo rand_str(6);  
  
?>
Copy after login


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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template