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

WBOY
Release: 2016-06-01 11:06:17
Original
1248 people have browsed it

本文实例讲述了php随机生成数字字母组合的方法。分享给大家供大家参考。具体如下:

直接上代码:

代码如下:

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         $str .= $chars[mt_rand(0, $lc)]; 
    }
    return $str;
}

例如随机生成 2 位 字母和数字组合
只需调用函数 并传参2即可。

echo getRandomString(2);
Copy after login

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

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

大写字母

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

希望本文所述对大家的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 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!