常用方法笔记

2021/3/8 Code

🎉 💯 常用的系统方法如下:

# 打印数据

<?php

/**
 * 打印数据
 * @param param 参数信息
 * @return array
 */
function v() {
    $param = func_get_args();
    foreach ($param as $v) {
        if ($v != false) {
            echo '<pre autocomplete="off" style="background-color:#272a32;color:#ffe4a6;font:15px/19px Consolas;padding:8px;width:90%;word-break:break-all;white-space:pre-wrap;">';
            var_dump($v);
            echo '</pre>';
        }
    }
    if (!in_array(false, $param)) exit() ;
}

/**
 * 打印数据
 * @param param 参数信息
 * @return array
 */
function p() {
    $param = func_get_args();
    foreach ($param as $v) {
        if ($v != false) {
            echo '<pre autocomplete="off" style="background-color:#272a32;color:#ffe4a6;font:15px/19px Consolas;padding:8px;width:90%;word-break:break-all;white-space:pre-wrap;">';
            var_dump($v);
            echo '</pre>';
        }
    }
    if (!in_array(false, $param)) exit() ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 文件大小转换

/**
 * 文件大小转换
 * @param param 文件大小
 * @return array
 */
function formatBytes($size) {
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2).$units[$i];
}
1
2
3
4
5
6
7
8
9
10

# 相对地址转换为绝对地址

/**
 * 相对地址转换为绝对地址
 * @param string $url
 * @return string
 */
function getRealUrl($url = '')
{
    if (stripos($url, 'http') !== false || stripos($url, 'https') !== false) {
        return $url;
    } else {
        if (config('domain')) {
            return config('domain') . $url;
        }
        return request()->domain() . $url;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 加密url参数

/**
 * 加密url参数
 * @param string $url
 * @return string
 */

//---------------以下为加密函数(复制过去就行了)-----------------
function keyED($txt, $encrypt_key){ //定义一个key
    $encrypt_key = md5($encrypt_key);
    $ctr = 0;
    $tmp = '';
    for ($i=0;$i<strlen($txt);$i++) {
        if ($ctr==strlen($encrypt_key))
        $ctr=0;
        $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
        $ctr++;
    }
    return $tmp;
}
function encrypt_urls($txt,$key) {
    $encrypt_key = md5(mt_rand(0,100));
    $ctr=0;
    $tmp = '';
    for ($i=0;$i<strlen($txt);$i++) {
        if ($ctr==strlen($encrypt_key))  
                    $ctr=0;
        $tmp.=substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
        $ctr++;
    }
    return keyED($tmp,$key);
}
function decrypt_urls($txt,$key) {
    $txt = keyED($txt,$key);
    $tmp = '';
    for ($i=0;$i<strlen($txt);$i++) {
        $md5 = substr($txt,$i,1);
        $i++;
        $tmp.= (substr($txt,$i,1) ^ $md5);
    }
    return $tmp;
}
// 加密url参数
function encrypt_url($url,$key) {
    return rawurlencode(base64_encode(encrypt_urls($url,$key)));
}
// 解密url参数
function decrypt_url($url,$key) {
    return decrypt_urls(base64_decode(rawurldecode($url)),$key);
}
function geturl($str,$key) {
    $str = decrypt_url($str,$key);
    $url_array = explode('&',$str);
    if (is_array($url_array)) {
        foreach ($url_array as $var) {
            $var_array = explode('=',$var);
            $vars[$var_array[0]]=$var_array[1];
        }
    }
    return $vars;
}

$key_url_md_5 = 'asr.wuhuabamenapp.com/share/md5';
$a = encrypt_url($infoContent, $key_url_md_5); // 加密
$b = geturl($a, $key_url_md_5); // 解密
//---------------以上为加密函数-结束(复制过去就行了)-----------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

# 写入日志

/**
 * 写入日志
 * @param string $url
 * @return string
 */
function writeLog($content, $dir = '')
{
    if (is_array($content)) {
        $content = json_encode($content);
    }

    $dateS = date("Ymd", time());
    $dataE = date("Y-m-d H:i:s", time());
    $content = $dataE . ": " . "\r\n" . $content . "\r\n";
    $path = dirname(__FILE__) . "/../public/logs/";

    if ($dir) {
        $dir = strtoupper($dir);
    } else {
        $dir = 'COMMON';//公共日志
    }
    if (!is_dir($path)) {
        mkdir(iconv('UTF-8', 'GBK', $path), 0777, true);
    }
    $path .= $dir;
    if (!is_dir($path)) {
        mkdir(iconv('UTF-8', 'GBK', $path), 0777, true);
    }
    error_log($content, 3, $path . "/" . $dateS . "errors.log");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 唯一uniqid不重复

/**
 * 唯一uniqid不重复
 * @param string $url
 * @return string
 */
function uniqidReal($lenght = 13)
{
    // uniqid gives 13 chars, but you could adjust it to your needs.
    if (function_exists("random_bytes")) {
        $bytes = random_bytes(ceil($lenght / 2));
    } elseif (function_exists("openssl_random_pseudo_bytes")) {
        $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
    } else {
        throw new Exception("no cryptographically secure random function available");
    }
    return substr(bin2hex($bytes), 0, $lenght);
}
// 进行检验
$arr = [];
for ($i = 1; $i <= 1000000; $i++) {
    $arr[] = uniqidReal(16);
}
var_dump(count($arr), count(array_unique($arr)));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 类型整合

/**
 * 类型整合(处理入库数据)
 *
 * @param array $array 数组
 * @param array 参数 描述
 * @param int $type 0=交集,1=处理mysql数据,2=1的另外一种写法
 * @return json Response
 */
function dataSql($array, $data, $type = 0)
{
    $info = [];
    switch ($type) {
        case '1':
            foreach ($data as $v) {
                $info[$v] = $array[$v];
                if(strpos($v, ' as ') !== false){
                    $explode = explode(' as ', $v);
                    $info[$explode[1]] = $array[$explode[0]];
                }
                if(strpos($v, ' json ') !== false){
                    $explode = explode(' json ', $v);
                    $info[$explode[1]] = $array[$explode[0]];
                }
            }
            break;
        case '2':
            $data = [['id', 'as', 'device_id'], 'name', 'uid', 'local_key', 'category', 'product_id', 'product_name', 'sub', 'uuid', 'owner_id', 'online', ['status', 'json', 'status'], 'active_time', 'biz_type', 'icon', 'ip', 'create_time', 'update_time', 'created_at'];
            foreach ($data as $v) {
                if (!is_array($v)) $info[$v] = $array[$v];
                if ($v[1] == 'as') $info[$v[2]] = $array[$v[0]];
                if ($v[1] == 'json') $info[$v[2]] = json_encode($array[$v[0]], 256);
            }
            break;
        default:
            $info = array_intersect_key($array, array_flip($data));
            break;
    }
    return $info;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 字符串存在与否

/**
 * 字符串存在与否
 *
 * @return json Response
 */
function strpos()
{
    // 判断是否是微信内打开
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($agent,'micromessenger') === false) return '请在微信内扫码打开!';
}
1
2
3
4
5
6
7
8
9
10
11

# 伪随机数产生器

/**
 * 伪随机数产生器
 *
 * @param string $lenght 进制长度
 * @return string Response
 */
public function uniqidReal($lenght = 13) {
    // uniqid gives 13 chars, but you could adjust it to your needs.
    if (function_exists("random_bytes")) {
        $bytes = random_bytes(ceil($lenght / 2));
    } elseif (function_exists("openssl_random_pseudo_bytes")) {
        $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
    } else {
        throw new Exception("no cryptographically secure random function available");
    }
    return substr(bin2hex($bytes), 0, $lenght);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 获取数组中指定的键值

/**
* 获取数组中指定的键值
* @param array $array 原数组
* @param array  $keys 要获取的键值
* @return array
*/
if (!function_exists('get_only_keys')) {

    function get_only_keys(array $array, array $keys, bool $hidden = false) {
        if ($hidden === true) {
            // 隐藏keys
            return array_diff_key($array, array_flip($keys));
        }
        return array_intersect_key($array, array_flip($keys));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 返回JSON

/**
 * 返回JSON
 *
 * @param int code 状态码
 * @param int data 数组
 * @param int msg 返回信息
 * @return json Response
 */
if (!function_exists("jsons")){
    function jsons($code = 0, $data = [], $msg = '操作成功!')
    {
        $return = [];
        http_response_code($code);    //设置返回头部
        $return['code'] = (int)$code;
        if ($return['code'] == 0) {
            $return['status'] = 'success!';
            $return['data'] = is_array($data) ? $data : ['info'=>$data];
            $return['msg'] = $msg;
        }else{
            $return['status'] = 'fail!';
            $return['msg'] = $data;
        }
        return json($return, JSON_UNESCAPED_UNICODE);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25