PHP generates random numbers, strings

Source: Internet
Author: User
Functionrand_string ($ len6, $ type & amp; #39; & amp; #39;, $ addChars & amp; #39; & amp; #39 ;) {$ str & amp; #39; & amp; #39; switch ($ type) {case0: $ chars & amp; #39;

Function rand_string ($ len = 6, $ type = '', $ addChars = ''){
$ Str = '';
Switch ($ type ){
Case 0:
$ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'. $ addChars;
Break;
Case 1:
$ Chars = str_repeat ('20140901', 3 );
Break;
Case 2:
$ Chars = 'abcdefghijklmnopqrstuvwxy'. $ addChars;
Break;
Case 3:
$ Chars = 'abcdefghijklmnopqrstuvwxy'. $ addChars;
Break;
Default:
// OOLl and number 01 are removed by default. use the addChars parameter to add them.
$ Chars = 'abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz23456789 '. $ addChars;
Break;
}
If ($ len> 10) {// The number of digits is too long to repeat the string for 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 {
// Random Chinese characters
For ($ I = 0; $ I <$ len; $ I ++ ){
$ Str. = msubstr ($ chars, floor (mt_rand (0, mb_strlen ($ chars, 'utf-8')-1), 1 );
}
}
Return $ str;
}

 

 

From the column lpdx111

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.