php随机生成字符串一些方法总结
前面有讲过生成随机密码,下面我再来给大家介绍一些常用的生成随机字符串的函数吧,这些都是我们自定义的函数,当然也有系统自带函数了,不过都比较简单了。
mt_rand函数
例子
在本例中,我们会返回一些随机数:
代码如下 | 复制代码 |
echo(mt_rand()); 3150906288 |
下面我们来看看mt_rand函数的实例吧。
代码如下 | 复制代码 |
function roll () { echo roll(); function roll ($sides) { } |
上面都只能生成简单的纯数字,不能是字母或数字与字母的,下面我们需用到自定义函数了
代码如下 | 复制代码 |
function genRandomString($len) { $charsLen = count($chars) - 1; shuffle($chars); // 将数组打乱 $str = genRandomString(25); echo $str;
|
例
默认生成的随机字符串长度为5,生成的字符串包含:数字+大写字母
函数功能:
1、生成指定长度的随机字符串
2、灵活选择生成的随机字符串的复杂度
代码如下 | 复制代码 |
/** |
例
1、预置一个的字符数组 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、通过array_rand()从数组 $chars 中随机选出 $length 个元素
3、根据已获取的键名数组 $keys,从数组 $chars 取出字符拼接字符串。该方法的缺点是相同的字符不会重复取。
代码如下 | 复制代码 |
function make_password( $length = 8 ) // 在 $chars 中随机取 $length 个数组元素键名 $password = ''; return $password; |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools
