php生成随机数/生成随机字符串的方法小结【5种方法】
作者:TANKING 时间:2023-09-05 20:23:21
本文实例讲述了php生成随机数/生成随机字符串的方法。分享给大家供大家参考,具体如下:
有时候我们的项目需要生成随机数,但是又不想那么麻烦,那么我们可以使用下面这5种方法生成。
第一种:mt_rand()
<?php
function GetRandStr($length){
//字符组合
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len = strlen($str)-1;
$randstr = '';
for ($i=0;$i<$length;$i++) {
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
//传入长度
$number = GetRandStr(6);
echo $number;
?>
第二种:array_rand()数组
<?php
function make_password($length)
{
// 密码字符集,可任意添加你需要的字符
$str = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
// 在 $str 中随机取 $length 个数组元素键名
$keys = array_rand($str, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$password .= $str[$keys[$i]];
}
return $password;
}
echo make_password(6);
?>
第三种:把字符串打乱,然后返回其中的一小截
<?php
function getrandstr($length){
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
$randStr = str_shuffle($str);//打乱字符串
$rands= substr($randStr,0,$length);//substr(string,start,length);返回字符串的一部分
return $rands;
}
echo getrandstr(6);
?>
第四种:返回任意随机数
<?php
//返回1000-9999其中的一个随机数
echo rand(1000,9999);
?>
第五种:对时间戳进行MD5加密,截取其中一部分
<?php
function token($length){
$str = md5(time());
$token = substr($str,5,$length);
return $token;
}
echo token(6);
?>
5种方法,任你选择,当然还有很多方法,例如多个字符串进行拼接,最后做md5加密或SHA1加密,然后返回字符串,这种比较普遍用于token验证或签名验证。
来源:https://segmentfault.com/a/1190000022718001
标签:php,生成随机数,生成随机字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中字符串的编码与解码详析
2022-11-15 23:24:12
![](https://img.aspxhome.com/file/2023/1/85171_0s.jpg)
Python实现将xml导入至excel
2023-10-01 06:17:45
![](https://img.aspxhome.com/file/2023/5/95605_0s.jpg)
go mode tidy出现报错go: warning: “all“ matched no packages的解决方法
2024-02-04 22:35:14
![](https://img.aspxhome.com/file/2023/5/108085_0s.png)
在Python 字典中一键对应多个值的实例
2023-07-25 23:45:02
vscode 左侧扩展活动栏内容消失的问题及解决方法
2022-01-12 04:26:35
![](https://img.aspxhome.com/file/2023/6/120516_0s.jpg)
bootstrapValidator bootstrap-select验证不可用的解决办法
2024-04-10 13:53:06
设计的俗化特征
2010-04-08 16:07:00
![](https://img.aspxhome.com/file/UploadPic/20104/8/01-49s.jpg)
对Python字符串中的换行符和制表符介绍
2021-11-11 15:45:29
vantUI 获得piker选中值的自定义ID操作
2024-04-10 13:48:42
![](https://img.aspxhome.com/file/2023/5/139685_0s.jpg)
牢不可破的九宫格布局
2009-07-24 12:40:00
![](https://img.aspxhome.com/file/UploadPic/20097/24/jiougongge1-43s.gif)
MySQL主从延迟现象及原理分析详解
2024-01-13 23:22:03
![](https://img.aspxhome.com/file/2023/7/111377_0s.png)
Python图像处理之图像金字塔详解
2022-03-11 10:35:04
![](https://img.aspxhome.com/file/2023/2/107512_0s.png)
python实现弹窗祝福效果
2021-09-08 04:22:15
![](https://img.aspxhome.com/file/2023/2/70792_0s.jpg)
Python3.X 线程中信号量的使用方法示例
2022-06-12 12:07:13
![](https://img.aspxhome.com/file/2023/3/107243_0s.png)
详解爬虫被封的问题
2021-01-29 15:21:16
![](https://img.aspxhome.com/file/2023/7/130587_0s.jpg)
Go语言七篇入门教程七GC垃圾回收三色标记
2024-05-05 09:28:45
![](https://img.aspxhome.com/file/2023/3/128683_0s.gif)
Python学习笔记之文件的读写操作实例分析
2023-08-28 04:29:06
导入tensorflow:ImportError: libcublas.so.9.0 报错
2023-07-07 11:44:46
![](https://img.aspxhome.com/file/2023/7/68257_0s.jpg)
Yii框架学习笔记之应用组件操作示例
2024-05-11 09:23:07
Pytorch中使用ImageFolder读取数据集时忽略特定文件
2021-08-09 01:18:24