封装方法

生成随机数:数字+字母

 //生成随机数:数字+字母(简单版)
    function sys_randomkeys($length){   
        $key='';
        $pattern = '123456789abcdefghijklmnopqrstuvwxyz';  
        for($i=0;$i<$length;$i++)   
        {   
            $key .= $pattern{mt_rand(0,35)};    //生成php随机数   
        }   
        return $key;   
    }   

生成随机字符串(复杂版)


// 生成随机字符串
function sys_str_randstr($len, $type = ""){
    $int = "123456789";
    $num = "0123456789";
    $lower = "abcdefghijklmnopqrstuvwxyz";
    $upper = strtoupper($lower);
    $str = $lower.$upper;
    $code = "abcdefghkmnprstuvwyABCDEFGHJKMNPRSTUVWY23456789";
    $hash = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".$num;
    $types = array("int","str","lower","upper","code","num","hash");
    $string = in_array($type,$types) ? $$type : ($num.$str);
    $return = "";
    for($i = 0; $i < $len; $i++){
        if($i > 0 && $type == "int") $string = $num;
        $return .= substr($string,rand(0,strlen($string)-1),1);
    }

    return (string)$return;
}

取得随机字符串(长度可自定义)


/* 作用:取得随机字符串
 * 参数:
  1、(int)$length = 32 #随机字符长度
  2、(int)$mode = 0    #随机字符类型,
  0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,
  4为大小写字母,5为大写字母和数字,6为小写字母和数字
 * 返回:取得的字符串
 */

function get_code($length = 32, $mode = 0) {//获取随机验证码函数
    switch ($mode) {
        case '1':
            $str = '0123456789';
            break;
        case '2':
            $str = 'abcdefghijklmnopqrstuvwxyz';
            break;
        case '3':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case '4':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
            break;
        case '5':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
            break;
        case '6':
            $str = 'abcdefghijklmnopqrstuvwxyz1234567890';
            break;
        default:
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
            break;
    }
    $checkstr = '';
    $len = strlen($str) - 1;
    for ($i = 0; $i < $length; $i++) {
        //$num=rand(0,$len);//产生一个0到$len之间的随机数
        $num = mt_rand(0, $len); //产生一个0到$len之间的随机数
        $checkstr.=$str[$num];
    }
    return $checkstr;
}

判断字符串是否包含在数组中(默认不区分大小写)


// 判断字符串是否包含在数组中(默认不区分大小写)
function sys_str_in_array($str, $arr, $case = false, $type = false){
    if ($case == false) {
        $str = strtolower($str);
        foreach ($arr as $k => $v) {
            $arr[$k] = strtolower($v);
        }
    }

    return in_array($str, $arr, $type);
}

把过长的字符只显示规定的几个后面用省略号代替

//把过长的字符只显示规定的几个后面用省略号代替
   function sys_cut_str($str,$len,$suffix="..."){
        if(function_exists('mb_substr')){
            if(strlen($str) > $len){
                $str= mb_substr($str,0,$len).$suffix;
            }
            return $str;
        }else{
            if(strlen($str) > $len){
                $str= substr($str,0,$len).$suffix;
            }
            return $str;
        }         
   }
Last modification:January 18th, 2019 at 05:12 pm
如果觉得我的文章对你有用,请随意赞赏