首頁 > php教程 > PHP源码 > 分享一個php隨機產生字串的函數

分享一個php隨機產生字串的函數

巴扎黑
發布: 2016-11-07 10:46:22
原創
1312 人瀏覽過

分享一個php隨機產生字串的函數

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/**

 * 随机字符

 * @param number $length 长度

 * @param string $type 类型

 * @param number $convert 转换大小写

 * @return string

 */

function random($length=6, $type='string', $convert=0){

  

    $config = array(

        'number'=>'1234567890',

        'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

        'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',

        'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

    );

      

    if(!isset($config[$type])) $type = 'string';

    $string = $config[$type];

      

    $code = '';

    $strlen = strlen($string) -1;

    for($i = 0; $i < $length; $i++){

        $code .= $string{mt_rand(0, $strlen)};

    }

    if(!empty($convert)){

        $code = ($convert > 0)? strtoupper($code) : strtolower($code);

    }

    return $code;

}

登入後複製

   

具體如何使用,請看程式碼備註額


相關標籤:
php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板