Last active
June 18, 2020 03:53
-
-
Save GithubMrxia/4621f06a1fc351a3f0a6a2cc069881c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 生成流水号 | |
if (! function_exists('generateSN')) { | |
function generateSN($prefix = '', int $length = 30) { | |
if (empty($prefix)) { | |
$prefix = 'PF'; | |
} | |
$length = max(20, $length); | |
return str_pad($prefix . uniqid(date('YmdHis')), $length, mt_rand(0, 10000)); | |
} | |
} | |
/** | |
* 格式化金额 只保留小数两位 | |
* @param float $amount | |
* @param bool $lose 为false 第3位>0则第2位加1 true 第二位不变 | |
* @return float | |
*/ | |
if (! function_exists('formatAmount')) { | |
function formatAmount($amount, bool $lose = false) { | |
if ($lose) { | |
return bcdiv(intval(bcmul($amount, 100, 2)), 100, 2); | |
} else { | |
return bcdiv(ceil(bcmul($amount, 100, 2)), 100, 2); | |
} | |
} | |
} | |
/** | |
* | |
* 加密姓名 两位只加密第二位,超过两位只保留第一位和最后一位 | |
* | |
* @param string $name | |
* @return string | |
*/ | |
if (! function_exists('encryptName')) { | |
function encryptName(string $name) { | |
$str = mb_substr($name, 0, 1).'*'; | |
if (mb_strlen($name) > 2) { | |
return $str.mb_substr($name, -1, 1); | |
} | |
return $str; | |
} | |
} | |
/** | |
* 姓名加密 显示前1位,后1位,中间加密 | |
* | |
* @param string $mobile | |
* @return string | |
*/ | |
if (! function_exists('encryptMobile')) { | |
function encryptMobile(string $mobile) { | |
return substr($mobile, 0, 3). '****' . substr($mobile, -4, 4); | |
} | |
} | |
/** | |
* 手机号加密 显示前3位,后4位,中间加密 | |
* | |
* @param string $mobile | |
* @return string | |
*/ | |
if (! function_exists('encryptMobile')) { | |
function encryptMobile(string $mobile) { | |
return substr($mobile, 0, 3). '****' . substr($mobile, -4, 4); | |
} | |
} | |
/** | |
* 身份证加密 显示前4位,后4位,中间加密 | |
* | |
* @param string $certNo | |
* @return string | |
*/ | |
if(! function_exists('encryptCertNo')) { | |
function encryptCertNo(string $certNo){ | |
return substr($certNo, 0, 4) . '********' . substr($certNo, -4, 4); | |
} | |
} | |
/** | |
* 根据身份证号计算用户年龄 | |
* | |
* @param string $certNo | |
* @return integer | |
*/ | |
if (! function_exists('userAge')) { | |
function userAge(string $certNo) { | |
return ((int)date('Y') - (int)substr($certNo, 6, 4)); | |
} | |
} | |
/** | |
* 转码uft8 | |
* | |
* @param string $str | |
* @return string | |
*/ | |
if (!function_exists('strToUtf8')) { | |
function strToUtf8(string $str = '') { | |
$currentEncode = mb_detect_encoding($str, ["ASCII", "GB2312", "GBK", 'BIG5', 'UTF-8', 'CP936']); | |
return mb_convert_encoding($str, 'UTF-8', $currentEncode); | |
} | |
} | |
/** | |
* 过滤字符串中的空格 | |
* | |
* @param string $str | |
* @return string | |
*/ | |
if (!function_exists('filterStrSpace')) { | |
function filterStrSpace(string $str) | |
{ | |
return str_replace([' ', chr(194) . chr(160)], '', $str); | |
} | |
} | |
/** | |
* 过滤字符串中的特殊字符 | |
*/ | |
if (!function_exists('filterStrSpecialChar')) { | |
function filterStrSpecialChar(string $str) | |
{ | |
$search = [' ', chr(194) . chr(160), "\n", "\r", "\t", "\r\n", "\f", "\v"]; | |
return str_replace($search, '', $str); | |
} | |
} | |
// 生成流水号 | |
if (!function_exists('generateOrderNo')) { | |
function generateOrderNo() | |
{ | |
$letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
$year = date('Y'); | |
$month = date('m'); | |
$day = date('d'); | |
$hour = date('H'); | |
$minute = date('i'); | |
$second = date('s'); | |
$date = $letter[substr($year, 0, 2)] . $letter[substr($year, 2, 2)] . $letter[$month] . $letter[$day] . $letter[$hour] . $letter[$minute] . $letter[$second]; | |
$lastStr = ''; | |
$len = strlen($letter); | |
for ($i = 0; $i < 5; $i++) { | |
$lastStr .= $letter[mt_rand(0, $len - 1)]; | |
} | |
return $date . $lastStr; | |
} | |
} | |
if (!function_exists('arrayChunk')) { | |
/** | |
* 数组切分处理 | |
* | |
* @param array $arr | |
* @param integer $size | |
* @param callable $callback | |
* @return void | |
*/ | |
function arrayChunk(array $arr, int $size, callable $callback): void | |
{ | |
$chunk = array_chunk($arr, $size); | |
foreach ($chunk as $value) { | |
call_user_func($callback, $value); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment