- 隐藏手机号中间4位替换成*
if (!function_exists('hide_mobile')) {
function hide_mobile(string $mobile)
{
return substr_replace($mobile, '****', 3, 4);
}
}
- 判断是否是手机号
if (!function_exists('is_mobile')) {
function is_mobile($num)
{
return preg_match('/^1(3|4|5|7|8)\d{9}$/', $num);
}
}
- 隐藏身份证号中间11位替换成*
if (!function_exists('hide_id_card')) {
function hide_id_card(string $idCard)
{
return substr_replace($idCard, '*******', 6, 6);
}
}
- 判断是否是邮箱
if (!function_exists('is_email')) {
function is_email($email)
{
// 正则匹配邮箱格式
$pattern = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
if (!preg_match($pattern, $email)) {
return false;
} else {
return true;
}
}
}
- 生成随机数
if (!function_exists('random_num')){
function random_num($len = 6)
{
$chars = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9"
];
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
}
- 生成订单号
if (!function_exists('get_order_num')) {
function get_order_num($prefix = '')
{
$micro = substr(microtime(), 2, 3);
return $prefix . date("YmdHis") . $micro . rand(100000, 999999);
}
}
- 生成加密唯一ID
if (!function_exists('md5_unique_id')) {
function md5_unique_id(): string
{
// uniqid()函数基于以微秒计的当前时间,生成一个唯一的 ID
return md5(uniqid());
}
}
- 判断获取的数据是否为空
if (!function_exists('is_empty')) {
function is_empty($value)
{
// 判断是否存在该值
if (!isset($value)) {
return true;
}
// 判断是否为empty
if (empty($value)) {
return true;
}
// 判断是否为null
if ($value === null) {
return true;
}
// 判断是否为空字符串
if (trim($value) === '') {
return true;
}
// 默认返回false
return false;
}
}
- 获取服务端IP地址
if (!function_exists('get_server_ip')) {
function get_server_ip()
{
if (isset($_SERVER)) {
if ($_SERVER['SERVER_ADDR']) {
$server_ip = $_SERVER['SERVER_ADDR'];
} else {
$server_ip = $_SERVER['LOCAL_ADDR'];
}
} else {
$server_ip = getenv('SERVER_ADDR');
}
return $server_ip;
}
}
- 获取客户端IP地址
if (!function_exists('get_client_ip')) {
function get_client_ip($type = 0, $adv = false)
{
$type = $type ? 1 : 0;
static $ip = null;
if ($ip !== null) {
return $ip[$type];
}
if ($adv) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) {
unset($arr[$pos]);
}
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf("%u", ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
}
- 字符串加密
if (!function_exists('encrypt')) {
function encrypt($text): string
{
$key = '789hd123cb99978jads0jnk86';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+';
$nh = rand(0, 64);
$ch = $chars[$nh];
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$text = base64_encode($text);
$tmp = '';
$k = 0;
for ($i = 0; $i < strlen($text); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh + strpos($chars, $text[$i]) + ord($mdKey[$k++])) % 64;
$tmp .= $chars[$j];
}
return $ch . $tmp;
}
}
- 字符串解密
if (!function_exists('encrypt')) {
function decrypt($text)
{
$key = '789hd123cb99978jads0jnk86';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+';
$ch = $text[0];
$nh = strpos($chars, $ch);
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$text = substr($text, 1);
$tmp = '';
$k = 0;
for ($i = 0; $i < strlen($text); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars, $text[$i]) - $nh - ord($mdKey[$k++]);
while ($j < 0)
$j += 64;
$tmp .= $chars[$j];
}
return base64_decode($tmp);
}
}
- 数组转xml
if (!function_exists('array_to_xml')) {
function array_to_xml($arr, $ignore = true, $level = 1)
{
$s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
$space = str_repeat("\t", $level);
foreach ($arr as $k => $v) {
if (!is_array($v)) {
$s .= $space . "<item id=\"$k\">" . ($ignore ? '<![CDATA[' : '') . $v . ($ignore ? ']]>' : '')
. "</item>\r\n";
} else {
$s .= $space . "<item id=\"$k\">\r\n" . array2xml($v, $ignore, $level + 1) . $space . "</item>\r\n";
}
}
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return $level == 1 ? $s . "</root>" : $s;
}
}
- xml转数组
if (!function_exists('xml_to_array')) {
function xml_to_array(&$xml)
{
$xml = "<xml>";
foreach ($xml as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
}
- 多维数组合并
if (!function_exists('array_merge_multiple')) {
function array_merge_multiple($array1, $array2)
{
$merge = $array1 + $array2;
$data = [];
foreach ($merge as $key => $val) {
if (isset($array1[$key])
&& is_array($array1[$key])
&& isset($array2[$key])
&& is_array($array2[$key])
) {
$data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
} else {
$data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
}
}
return $data;
}
}
- 获取数组某个字段的所有值
if (!function_exists('array_key_value')) {
function array_key_value($arr, $name = "")
{
$result = [];
if ($arr) {
foreach ($arr as $key => $val) {
if ($name) {
$result[] = $val[$name];
} else {
$result[] = $key;
}
}
}
$result = array_unique($result);
return $result;
}
}
- 对象转数组
if (!function_exists('object_array')) {
function object_array($object)
{
//先编码成json字符串,再解码成数组
return json_decode(json_encode($object), true);
}
}
- 获取毫秒时间戳
if (!function_exists('milli_timestamp')) {
function milli_timestamp(): int
{
return (int)(microtime(true) * 1000);
}
}
- 数组排序
if (!function_exists('array_sort')) {
function array_sort($arr, $keys, $desc = false)
{
$key_value = $new_array = array();
foreach ($arr as $k => $v) {
$key_value[$k] = $v[$keys];
}
if ($desc) {
arsort($key_value);
} else {
asort($key_value);
}
reset($key_value);
foreach ($key_value as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
}
- curl(get请求)
if (!function_exists('curl_get')) {
function curl_get($url, $data = [])
{
if (!empty($data)) {
$url = $url . '?' . http_build_query($data);
}
// 初始化
$ch = curl_init();
// 设置抓取的url
curl_setopt($ch, CURLOPT_URL, $url);
// 设置头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HEADER, false);
// 是否要求返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 执行命令
$result = curl_exec($ch);
// 关闭URL请求(释放句柄)
curl_close($ch);
return $result;
}
}
- curl(post请求)
if (!function_exists('curl_post')) {
function curl_post($url, $data)
{
// 初始化
$ch = curl_init();
// 设置post方式提交
curl_setopt($ch, CURLOPT_POST, 1);
// 设置头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HEADER, 0);
// 是否要求返回数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 设置抓取的url
curl_setopt($ch, CURLOPT_URL, $url);
// 提交的数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 执行命令
$result = curl_exec($ch);
// 关闭URL请求(释放句柄)
curl_close($ch);
return $result;
}
}
- 数字金额转大写
if (!function_exists('num_to_rmb')) {
function num_to_rmb($num)
{
$c1 = "零壹贰叁肆伍陆柒捌玖";
$c2 = "分角元拾佰仟万拾佰仟亿";
$num = round($num, 2);
$num = $num * 100;
if (strlen($num) > 10) {
return "oh,sorry,the number is too long!";
}
$i = 0;
$c = "";
while (1) {
if ($i == 0) {
$n = substr($num, strlen($num) - 1, 1);
} else {
$n = $num % 10;
}
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
$num = $num / 10;
$num = (int)$num;
if ($num == 0) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
$m = substr($c, $j, 6);
if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
$left = substr($c, 0, $j);
$right = substr($c, $j + 3);
$c = $left . $right;
$j = $j - 3;
$slen = $slen - 3;
}
$j = $j + 3;
}
if (substr($c, strlen($c) - 3, 3) == '零') {
$c = substr($c, 0, strlen($c) - 3);
}
return $c . "整";
}
}
- 生成action_token
/**
* 生成action_token
* $valid_time 有效时间 : 秒
*/
if (!function_exists('create_action_token')){
function create_action_token($userid,$valid_time) {
$time = time();
$end_time = $time + $valid_time;
$info = $userid . '-' . $time . '-' . $end_time;
//根据以上信息信息生成签名(密钥为 SIGNATURE 自定义全局常量)
$signature = hash_hmac('md5', $info, SIGNATURE);
//最后将这两部分拼接起来,得到最终的Token字符串
$token = $info . '-' . $signature;
return $token;
}
}
- 检查action_token
/**
* 检查action_token
*/
if (!function_exists('check_action_token')){
function check_action_token($token)
{
if (!isset($token) || empty($token)) {
return [
'code' => 400,
'msg' => 'illegal request'
];
}
//对比token
$explode = explode('-', $token); //以.分割token为数组
if (!empty($explode[0]) && !empty($explode[1]) && !empty($explode[2]) && !empty($explode[3])) {
$info = $explode[0] . '-' . $explode[1] . '-' . $explode[2]; //信息部分
$true_signature = hash_hmac('md5', $info, SIGNATURE); //正确的签名
if (time() > $explode[2]) {
return [
'code' => 401,
'msg' => 'action token invalid'
];
}
if ($true_signature == $explode[3]) {
return [
'code' => 200,
'msg' => 'action token legal'
];
} else {
return [
'code' => 401,
'msg' => 'action token invalid'
];
}
} else {
return [
'code' => 401,
'msg' => 'action token invalid'
];
}
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END