PHP之生成唯一字符串

生成唯一字符串的PHP实现
 /**
  * [生成唯一字符串]
  * @ 0-存数字字符串;1-小写字母字符串;2-大写字母字符串;3-大小写数字字符串;4-字符;
  *   5-数字,小写,大写,字符混合
  * @param  integer $type   [字符串的类型]
  * @param  integer $length [字符串的长度]
  * @param  integer $time   [是否带时间1-带,0-不带]
  * @return [string]  $str    [返回唯一字符串]
  */
public static function randSole($type = 0,$length = 12,$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;
                 }else{
                     $str .= rand(0,9);
                 }
             }
             break;
         case 1:
             for((int)$i = 0;$i <= $length;$i++){
                 if(mb_strlen($str) == $length){
                     $str = $str;
                 }else{
                     $rand = "qwertyuioplkjhgfdsazxcvbnm";
                     $str .= $rand{mt_rand(0,26)};
                 }
             }
             break;
         case 2:
             for((int)$i = 0;$i <= $length;$i++){
                 if(mb_strlen($str) == $length){
                     $str = $str;
                 }else{
                     $rand = "QWERTYUIOPLKJHGFDSAZXCVBNM";
                     $str .= $rand{mt_rand(0,26)};
                 }
             }
             break;
         case 3:
             for((int)$i = 0;$i <= $length;$i++){
                 if(mb_strlen($str) == $length){
                     $str = $str;
                 }else{
                     $rand = "123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
                     $str .= $rand{mt_rand(0,35)};
                 }
             }
             break;
         case 4:
             for((int)$i = 0;$i <= $length;$i++){
                 if(mb_strlen($str) == $length){
                     $str = $str;
                 }else{
                     $rand = "!@#$%^&*()_+=-~`";
                     $str .= $rand{mt_rand(0,17)};
                 }
             }
             break;
         case 5:
             for((int)$i = 0;$i <= $length;$i++){
                 if(mb_strlen($str) == $length){
                     $str = $str;
                 }else{
                     $rand = "123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_+=-~`";
                     $str .= $rand{mt_rand(0,52)};
                 }
             }
             break;
     }
     return $str;
 }

 

<?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、付费专栏及课程。

余额充值