码迷,mamicode.com
首页 > Web开发 > 详细

PHP固定长度字符串

时间:2019-12-20 15:28:34      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:ring   must   pad   shuf   数组   char   span   字符串   throw   

/**
 * 获取固定长度随机字符串
 * @param $n
 * @return string
 * @throws Exception
 */
function gf_rand_str($n) {
    if (!is_int($n)) {
        throw new Exception(‘argument must be int‘);
    }
    $alpha = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘;
    $str = ‘‘;
    for ($i=0; $i<$n; $i++) {
        $str .= $alpha[rand(0, 35)];
    }
    return $str;
}

前三位字母后三位数字:

function invite_num($len = 6)
{
    $en_chars = [
        "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z"
    ];
    $num_chars = [
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
    ];
    $en_shuf = shuffle($en_chars);    // 将数组打乱
    $num_shuf = shuffle($num_chars);
    $output = "";
    for ($i = 0; $i < 3; $i++) {
        $output .= $en_chars[mt_rand(0, $len)];
    }
    $output .= substr(getMicroSecondsTimestamp(), 11, 1);
    $output .= rand(10, 99);
    return $output;
}
function getMicroSecondsTimestamp()
{
    $time = microtime();
    return substr($time, 11, 10) . str_pad(substr($time, 0, 8) * 1000000,
            6, "0", STR_PAD_LEFT);
}

PHP固定长度字符串

标签:ring   must   pad   shuf   数组   char   span   字符串   throw   

原文地址:https://www.cnblogs.com/sgm4231/p/12073121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!