<?php function randpw($len=8,$format='ALL'){ $is_abc = $is_numer = 0; $password = $tmp =''; switch($format){ case 'ALL': $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break; case 'CHAR': $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; case 'NUMBER': $chars='0123456789'; break; default : $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break; } // www.jb51.net mt_srand((double)microtime()*1000000*getmypid()); while(strlen($password)<$len){ $tmp =substr($chars,(mt_rand()%strlen($chars)),1); if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )|| $format == 'CHAR'){ $is_numer = 1; } if(($is_abc <> 1 && preg_match('/[a-zA-Z]/',$tmp)) || $format == 'NUMBER'){ $is_abc = 1; } $password.= $tmp; } if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){ $password = randpw($len,$format); } return $password; } for($i = 0 ; $i < 10; $i++){ echo randpw(8,'NUMBER'); echo "<br>"; }
Whether it is a web application, a WAP or a mobile application, random numbers have their place. In several small projects I have recently come into contact with, I often need to deal with random numbers or random arrays. What I bring to you is RandomLib-master, a PHP library for generating random numbers. This library is very useful. You can download it directly if you need it.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
09Jul2017
This article summarizes 5 methods for generating non-repeating random numbers. Methods 1, 2, and 3 are my commonly used methods, and methods 4 and 5 come to Du Niang. There are still other methods, and you are welcome to tell me the missing methods, so that we can make progress together.
17Jan2019
PHP's internal method of generating random numbers is simpler than other methods and does not require additional configuration. It is the preferred method for generating random numbers. 1 rand function rand() function can generate random integers without adding any parameters. If you want to set the range of random numbers, you can set the values of min and max in the function.
06Mar2018
Random numbers are a very commonly used method. This article summarizes the methods of generating random numbers in PHP. It is divided into four methods and the comparison between each method. Readers in need can refer to it.
13Jul2016
Summary of methods for generating non-repeating random numbers in PHP, summary of generating random numbers in PHP. Summary of methods for generating non-repeating random numbers in PHP. Summary of generating random numbers in PHP. Whether it is a web application, a WAP or a mobile application, random numbers have their place. Several small items that I have been exposed to recently
29Jul2016
Generate random numbers: Generate random numbers using two methods of generating random numbers using PHP. Example code to output random IP: Share three methods of generating random numbers using PHP. Generate non-repeating random numbers between 1-10. PHP generates non-repeating random numbers. For example, friends in need can refer to it. How to generate non-repeating random numbers between 1-10 using php? Example 1, use shuffle function to generate random numbers.
18May2018
This time I will bring you some methods of generating random numbers in PHP and what are the precautions for generating random numbers in PHP. The following is a practical case, let's take a look.
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images