一些PHP生成唯一字符串的方法

form:http://www.imooc.com/article/13419

/** * 生成32位的唯一字符串 */
function unique_name()
{
    return md5(uniqid(mt_rand(), true));
}


/** * 生成毫/微秒的时间戳 */
function microtime_float()
{ /* 微秒 */
    list($usec, $sec) = explode(' ', microtime()); /* 非科学计数法 */
    return array('ms' => microtime(true) * 10000, 'mi' => sprintf("%.0f", ((float)$usec + (float)$sec) * 100000000),);
}


/** * 生成指定位数的随机数 */
function random_number($a = array('can_zero_start' => null, 'length' => null, 'prefix' => null, 'suffix' => null,))
{ /* 允许0开头 */
    $can_zero_start = isset($a['can_zero_start']) ? $a['can_zero_start'] : true;
    if ($can_zero_start) {
        $num = mt_rand(0, 9);
    } else {
        $num = mt_rand(1, 9);
    } /* 长度 */
    $length = isset($a['length']) ? $a['length'] : 8;
    for ($i = 0; $i < $length - 1; $i++) {
        $num .= mt_rand(0, 9);
    } /* 前缀 */
    $prefix = isset($a['prefix']) ? $a['prefix'] : null; /* 后缀 */
    $suffix = isset($a['suffix']) ? $a['suffix'] : null;
    return $prefix . $num . $suffix;
}


/** * 生成验证码 */
function verification_code($a = array('length' => null, 'number_case' => null, 'lower_case' => null, 'upper_case' => null, 'chinese' => null,))
{ /* 字符库 */
    $number_str_all = '0123456789';
    $number_str = '23456789';
    $lower_case_str = 'abcdefghijkmnpqrstuvwxyz';
    $upper_case_str = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
    $chinese_library = chinese_library(); /* 含有数字 */
    $number_case = isset($a['number_case']) ? $a['number_case'] : true; /* 含有小写字母 */
    $lower_case = isset($a['lower_case']) ? $a['lower_case'] : true; /* 含有大写字母 */
    $upper_case = isset($a['upper_case']) ? $a['upper_case'] : true; /* 中文 */
    $chinese = isset($a['chinese']) ? $a['chinese'] : false; /* 拼接字符库 */
    if ($chinese or ($number_case == false and $lower_case == false and $upper_case == false)) {
        $str = $chinese_library['str'];
    } elseif ($number_case == true and $lower_case == false and $upper_case == false) {
        $str = $number_str_all;
    } elseif ($number_case != true or $lower_case != false or $upper_case != false) {
        $str = null;
        if ($number_case) $str .= $number_str;
        if ($lower_case) $str .= $lower_case_str;
        if ($upper_case) $str .= $upper_case_str;
    } /* 长度 */
    $length = isset($a['length']) ? $a['length'] : 4; /* 拼接验证码 */
    $array_rand_val = array_rand_val(array('data' => $str, 'length' => $length, 'repeat' => true,));
    return array('code' => $array_rand_val['str'], 'code_arr' => $array_rand_val['arr'],);
}


/** * 生成中文验证码(包括候选码) */
function chinese_code($a = array('length_code' => null, 'length_candidate' => null,))
{ /* 验证码长度 */
    $length_code = isset($a['length_code']) ? $a['length_code'] : 2; /* 候选码长度 */
    $length_candidate = isset($a['length_candidate']) ? $a['length_candidate'] : 9; /* 如果填反了 */
    if ($length_code > $length_candidate) {
        list($length_code, $length_candidate) = array($length_candidate, $length_code);
    } /* 生成候选码 */
    $chinese_library = chinese_library();
    $candidate = array_rand_val(array('data' => $chinese_library['arr'], 'length' => $length_candidate, 'repeat' => false,)); /* 生成验证码 */
    $code = array_rand_val(array('data' => $candidate['arr'], 'length' => $length_code, 'repeat' => false,));
    return array('code_str' => $code['str'], 'candidate_str' => $candidate['str'], 'code_arr' => $code['arr'], 'candidate_arr' => $candidate['arr'],);
}


/** * 汉字库 */
function chinese_library()
{ /* 字符串 */
    $str = '的一是了不人在他有这个上们来到时大地为子中你说生国年着就那和要她出也得里后自以会家可下而过天去能对小多然于心学么之都好看起发当没成只如' . '事把还用第样道想作种开美总从无情己面最女但现前些所同手又行意动方期它头经长儿回位分爱老因很给名法间斯知世什两次使身者被高已亲其进此话常与活正感'; /* 数组 */
    preg_match_all('/./u', $str, $arr); /* 返回 字符串 & 数组 */
    return array('arr' => $arr[0], 'str' => $str,);
}


/** * 随机获取 数组/字符串 中的值/字符 */
function array_rand_val($a = array('data' => null, 'length' => null, 'repeat' => null,))
{ /* 转数组 */
    if (!isset($a['data'])) {
        $arr = array(1, 2, 3);
    } elseif (!is_array($a['data'])) {
        preg_match_all('/\S/u', $a['data'], $b);
        $arr = $b[0];
    } elseif (is_array($a['data'])) {
        $arr = $a['data'];
    } /* 可重复吗 */
    $repeat = isset($a['repeat']) ? $a['repeat'] : false; /* 获取个数 */
    $length = isset($a['length']) ? $a['length'] : 1;
    if ($repeat) {
        $return_arr = array();
        for ($i = 0; $i < $length; $i++) { /* 随机键名的值 */
            $return_arr[] = $arr[array_rand($arr)];
        }
    } elseif (!$repeat) { /* 打乱 */
        shuffle($arr); /* 获取 */
        $return_arr = array_slice($arr, 0, $length);
    }
    return array('arr' => $return_arr, 'str' => implode($return_arr),);
}
<?php /*  * This file is part of Hashids.  * (c) Ivan Akimov <ivan@barreleye.com>  * For the full copyright and license information, please view the LICENSE  * file that was distributed with this source code.  */ namespace Hashids; use RuntimeException; /**  * This is the math class.  * @author Vincent Klaiber <hello@vinkla.com>  * @author Jakub Kramarz <lenwe@lenwe.net>  */ class Math {     /**      * Add two arbitrary-length integers.      * @param string $a      * @param string $b      * @throws \RuntimeException      * @return string      */     public static function add($a, $b)     {         if (function_exists('gmp_add')) {             return gmp_add($a, $b);         }         if (function_exists('bcadd')) {             return bcadd($a, $b, 0);         }         throw new RuntimeException('Missing BC Math or GMP extension.');     }字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。在程序设计中,字符串(string)为符号或数值的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。通常以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。设p、q是两个串,求q在p中首次出现的位置的运算叫做模式匹配。串的两种最基本的存储方式是顺序存储方式和链接存储方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值