替换手机号码中间四位数字
function hide_phone($str){
$resstr = substr_replace($str,'****',3,4);
return $resstr;
}
子孙树 用于菜单整理 (一级)
function subTree($param, $pid = 0)
{
static $res = [];
foreach($param as $key=>$vo){
if( $pid == $vo['pid'] ){
$res[] = $vo;
if($vo['is_visible']==1){
$param[$key]['is_visible']=true;
}else{
$param[$key]['is_visible']=false;
}
subTree($param, $vo['category_id']);
}
}
return $res;
}
子孙树 用于菜单整理(多级)
function create_tree($data,$pk='id',$pid='pid'){
$tree = $list = [];
foreach ($data as $val) {
$list[$val[$pk]] = $val;
}
foreach ($list as $key =>$val){
if($val[$pid] == 0){
$tree[] = &$list[$key];
}else{
//找到其父类
$list[$val[$pid]]['children'][] = &$list[$key];
}
}
return $tree;
}
整理菜单树方法
function prepareMenu($param)
{
$parent = []; //父类
$child = []; //子类
foreach($param as $key=>$vo){
if($vo['pid'] == 0){
$vo['href'] = '#';
$parent[] = $vo;
}else{
$vo['href'] = url($vo['name']); //跳转地址
$child[] = $vo;
}
}
foreach($parent as $key=>$vo){
foreach($child as $k=>$v){
if($v['pid'] == $vo['id']){
$parent[$key]['child'][] = $v;
}
}
}
unset($child);
return $parent;
}
生成订单编号
function makeOrderNum()
{
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K');
$orderSn =
$yCode[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date(
'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
'%02d', rand(0, 99));
return $orderSn;
}
密码加密方式
function password($password,$psw_code='lasdjq2323')
{
return md5(md5($password) . md5($psw_code));
}
格式化字节大小
function format_bytes($size, $delimiter = '') {
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
return round($size, 2) . $delimiter . $units[$i];
}
生成随机字符串
function get_randomstr($length = 6) {
$chars = '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
截取字符串
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
if (function_exists("mb_substr"))
$slice = mb_substr($str, $start, $length, $charset);
elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
}
} else {
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
return $suffix ? $slice . '...' : $slice;
}
生成网址的二维码 返回图片地址
function Qrcode($token, $url, $size = 8){
$md5 = md5($token);
$dir = date('Ymd'). '/' . substr($md5, 0, 10) . '/';
$patch = 'qrcode/' . $dir;
if (!file_exists($patch)){
mkdir($patch, 0755, true);
}
$file = 'qrcode/' . $dir . $md5 . '.png';
$fileName = $file;
if (!file_exists($fileName)) {
$level = 'L';
$data = $url;
QRcode::png($data, $fileName, $level, $size, 2, true);
}
return $file;
}
网友评论