PHP生成随机或者唯一字符串

本文出至:新太潮流网络博客

/**
 * [生成随机字符串]
 * @E-mial wuliqiang_aa@163.com
 * @TIME   2017-04-07
 * @WEB    http://blog.iinu.com.cn
 * @param  integer $length [生成的长度]
 * @param  integer $type   [生成的类型]
 * @return [type]   str       [description]
 * @php 随机码类型:0,数字+大写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符
 */
function randCode($length = 5, $type = 0) { $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|"); if ($type == 0) { array_pop($arr); $string = implode("", $arr); } else if ($type == "-1") { $string = implode("", $arr); } else { $string = $arr[$type]; } $count = strlen($string) - 1; for ($i = 0; $i < $length; $i++) { $str[$i] = $string[rand(0, $count)]; $code .= $str[$i]; } return $code; } /** * [生成唯一字符串] * @E-mial wuliqiang_aa@163.com * @TIME 2017-04-07 * @WEB http://blog.iinu.com.cn * @ 0-存数字字符串;1-小写字母字符串;2-大写字母字符串;3-大小写数字字符串;4-字符; * 5-数字,小写,大写,字符混合 * @param integer $type [字符串的类型] * @param integer $length [字符串的长度] * @param integer $time [是否带时间1-带,0-不带] * @return [string] $str [返回唯一字符串] */ function randSole($type = 0,$length = 18,$time=0){ $str = $time == 0 ? '':date('YmdHis',time()); switch ($type) { case 0: for((int)$i = 0;$i <= $length;$i++){ if(mb_strlen($str) == $length){ $str = $str;

转载于:https://www.cnblogs.com/disoq/p/7509163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值