Home > php教程 > php手册 > php 随机生成数字字母组合的方法

php 随机生成数字字母组合的方法

WBOY
Release: 2018-09-27 16:41:34
Original
2460 people have browsed it

直接上代码:

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;  
}
Copy after login

例如随机生成 6 位 字母和数字组合 

只需调用函数 并传参6即可。

echo getRandomString(6);
Copy after login

如果仅仅是生成小写字母你可以使用类似方法

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

大写字母

echo chr(mt_rand(97, 122));
Copy after login
Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template