How to randomly generate a combination of numbers and letters in php, generate numbers and letters in php_PHP tutorial

WBOY
Release: 2016-07-13 10:02:48
Original
1339 people have browsed it

How to randomly generate a combination of numbers and letters in php, how to generate a combination of numbers and letters in php

The example in this article describes how to randomly generate a combination of numbers and letters in php. Share it with everyone for your reference. The details are as follows:

Go directly to the code:
Copy code The code is as follows: function getRandomString($len, $chars=null)
{
If (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
}  
mt_srand(10000000*(double)microtime());
for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i ){
          $str .= $chars[mt_rand(0, $lc)];
}
Return $str;
}

For example, randomly generate a 2-digit letter and number combination
Just call the function and pass parameter 2.

echo getRandomString(2);
Copy after login

If you only generate lowercase letters, you can use a similar method

echo chr(mt_rand(65, 90);
Copy after login

Capital letters

echo chr(mt_rand(97, 122));
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969495.htmlTechArticleHow to randomly generate a combination of numbers and letters in php, how to generate a combination of numbers and letters in php. This article describes how php randomly generates a combination of numbers and letters. method. Share it with everyone for your reference. The details are as follows:...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!