Home > Backend Development > PHP Tutorial > PHP generates random numbers and strings_PHP tutorial

PHP generates random numbers and strings_PHP tutorial

WBOY
Release: 2016-07-13 17:51:45
Original
953 people have browsed it

function rand_string($len = 6, $type = '', $addChars = '') {
$str = '';
switch ($type) {
case 0 :
                 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
break;
case 1 :
                $chars = str_repeat ('0123456789', 3);
break;
case 2:
               $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3:
                 $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
​​​​default:
                                                                                                                                                                                    oOLl and number 01 The easily confusing character oOLl and the number 01 are removed by default. To add them, please use the addChars parameter
                $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
break;
}  
If ($len > 10) { //The number of digits is too long and the string is repeated a certain number of times
$chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 );
}  
If ($type != 4) {
          $chars = str_shuffle ( $chars );
$str = substr ($chars, 0, $len);
} else {
                      // Chinese random words
for($i = 0; $i < $len; $i ++) {
$str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 );
         } 
}  
Return $str;
}

Excerpted from lpdx111’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478170.htmlTechArticlefunction rand_string($len = 6, $type = , $addChars = ) { $str = ; switch ($ type) { case 0 : $chars = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz . $addChars; break; case 1...
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