分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

php获取随机字符串的几种方法

发布时间:2023-09-06 02:11责任编辑:苏小强关键词:暂无标签

方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍)

 1 /** 2 ?* 获得随机字符串 3 ?* @param $len ????????????需要的长度 4 ?* @param $special ???????是否需要特殊符号 5 ?* @return string ??????返回随机字符串 6 ?*/ 7 function getRandomStr($len, $special=true){ 8 ????$chars = array( 9 ????????"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",10 ????????"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",11 ????????"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",12 ????????"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",13 ????????"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",14 ????????"3", "4", "5", "6", "7", "8", "9"15 ????);16 17 ????if($special){18 ????????$chars = array_merge($chars, array(19 ????????????"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",20 ????????????"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",21 ????????????"}", "<", ">", "~", "+", "=", ",", "."22 ????????));23 ????}24 25 ????$charsLen = count($chars) - 1;26 ????shuffle($chars); ???????????????????????????//打乱数组顺序27 ????$str = ‘‘;28 ????for($i=0; $i<$len; $i++){29 ????????$str .= $chars[mt_rand(0, $charsLen)]; ???//随机取出一位30 ????}31 ????return $str;32 }

方法二、str_shuffle函数(打乱字符串顺序)和mt_rand函数

1 //取随机10位字符串2 $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";3 $name=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),10);4 echo $name;

方法三、md5(),uniqid(),microtime()生成唯一的32位字符串

$uniqid = md5(uniqid(microtime(true),true));//microtime(true) ?返回系统当前时间戳的毫秒数

其他方法:

 1 /** 2 ?????* 方法一:获取随机字符串 3 ?????* @param number $length 长度 4 ?????* @param string $type 类型 5 ?????* @param number $convert 转换大小写 6 ?????* @return string 随机字符串 7 ?????*/ 8 ????function random($length = 6, $type = ‘string‘, $convert = 0) 9 ????{10 ????????$config = array(11 ????????????‘number‘ => ‘1234567890‘,12 ????????????‘letter‘ => ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘,13 ????????????‘string‘ => ‘abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789‘,14 ????????????‘all‘ => ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890‘15 ????????);16 ?17 ????????if (!isset($config[$type]))18 ????????????$type = ‘string‘;19 ????????$string = $config[$type];20 ?21 ????????$code = ‘‘;22 ????????$strlen = strlen($string) - 1;23 ????????for ($i = 0; $i < $length; $i++) {24 ????????????$code .= $string{mt_rand(0, $strlen)};25 ????????}26 ????????if (!empty($convert)) {27 ????????????$code = ($convert > 0) ? strtoupper($code) : strtolower($code);28 ????????}29 ????????return $code;30 ????}31 ?32 ????/**33 ?????* 方法二:获取随机字符串34 ?????* @param int $randLength 长度35 ?????* @param int $addtime 是否加入当前时间戳36 ?????* @param int $includenumber 是否包含数字37 ?????* @return string38 ?????*/39 ????function rand_str($randLength = 6, $addtime = 1, $includenumber = 0)40 ????{41 ????????if ($includenumber) {42 ????????????$chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789‘;43 ????????} else {44 ????????????$chars = ‘abcdefghijklmnopqrstuvwxyz‘;45 ????????}46 ????????$len = strlen($chars);47 ????????$randStr = ‘‘;48 ????????for ($i = 0; $i < $randLength; $i++) {49 ????????????$randStr .= $chars[mt_rand(0, $len - 1)];50 ????????}51 ????????$tokenvalue = $randStr;52 ????????if ($addtime) {53 ????????????$tokenvalue = $randStr . time();54 ????????}55 ????????return $tokenvalue;56 ????}

php获取随机字符串的几种方法

原文地址:https://www.cnblogs.com/myIvan/p/9533189.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved