Home >Backend Development >PHP Tutorial >PHP生成随机字符串的方法

PHP生成随机字符串的方法

WBOY
WBOYOriginal
2016-06-20 13:05:161028browse

PHP生成随机字符串的方法代码实现

< ?php<br />function genRandomString($len) <br />{ <br />    $chars = array( <br />        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",  <br />        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",  <br />        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",  <br />        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",  <br />        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",  <br />        "3", "4", "5", "6", "7", "8", "9" <br />    ); <br />    $charsLen = count($chars) - 1; <br /> <br />    shuffle($chars);    // 将数组打乱 <br />     <br />    $output = ""; <br />    for ($i=0; $i<$len; $i++) <br />    { <br />        $output .= $chars[mt_rand(0, $charsLen)]; <br />    } <br /> <br />    return $output; <br /> <br />} <br /> <br />$str = genRandomString(25); <br />$str .= "<br />"; <br />$str .= genRandomString(25); <br />$str .= "<br />"; <br />$str .= genRandomString(25); <br /> <br />echo $str; <br /> <br />?>



Statement:
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
Previous article:PHP多线程实现技术总结Next article:PHP-GTK 使用用途