- 价格格式化
```
/**
- 价格格式化 *
-
@param int float $price 价格 - @return string $price_format 输出价格 */ function ncPriceFormat(float $price=0) { return number_format($price, 2, ‘.’, ‘’); }
number_format 函数的参数解释如下:
- 第一个参数 $price:是要格式化的数值。
- 第二个参数 2:表示要保留的小数位数,这里设置为保留两位小数。
- 第三个参数 ‘.’:指定使用小数点 . 作为小数分隔符。
- 第四个参数 ‘‘:指定不使用千位分隔符。 ```
- 获取数组值
```
/**
- 获取数组值
- function description: 获取数组值,数组可以为多层,如果获取不到返回null *
- @param $arr array 需要获取值的数组
- @param $keys string 获取的键
-
@return mixed string - 如果数组为 $s[‘a’][‘c’][‘d’]= 2;
- 调用方法为 get_array_value($s,’a’,’c’,’d’); */ function get_array_value($arr=[], …$keys=’’) { if (count($keys) === 1) { $key = $keys[0]; if (!is_array($arr)) { return null; } if (!isset($arr[$key])) { return null; } return $arr[$key]; } $firstKey = array_shift($keys); return get_array_value(get_array_value($arr, $firstKey), …$keys); } ```
- 中文字串截取无乱码
```
/**
- 中文字串截取无乱码 *
- @param string $str 字符串
- @param int $start 开始位置
- @param int $length 长度
- @return string */ function getSubstr(string $string=’’, int $start=0, int $length=0) { if (mb_strlen($string, ‘utf-8’) > $length) { $str = mb_substr($string, $start, $length, ‘utf-8’); return $str . ‘…’; } else { return $string; } } ```
- 加解密函数
```
/**
- 加密函数 *
- @param string $txt 需要加密的字符串
- @param string $key 密钥
- @return string 返回加密结果 */ function encryption(string $txt=’’, string $key = ‘’) { if (empty($txt)) return $txt; if (empty($key)) $key = md5(‘AUTH_CODE’); $chars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.”; $ikey = “-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm”; $nh1 = rand(0, 64); $nh2 = rand(0, 64); $nh3 = rand(0, 64); $ch1 = $chars[$nh1]; $ch2 = $chars[$nh2]; $ch3 = $chars[$nh3]; $nhnum = $nh1 + $nh2 + $nh3; $knum = 0; $i = 0; while (isset($key[$i])) $knum += ord($key[$i++]); $mdKey = substr(md5(md5(md5($key . $ch1) . $ch2 . $ikey) . $ch3), $nhnum % 8, $knum % 8 + 16); $txt = base64_encode(time() . ‘’ . $txt); $txt = str_replace(array(‘+’, ‘/’, ‘=’), array(‘-‘, ‘_’, ‘.’), $txt); $tmp = ‘’; $j = 0; $k = 0; $tlen = strlen($txt); $klen = strlen($mdKey); for ($i = 0; $i < $tlen; $i++) { $k = $k == $klen ? 0 : $k; $j = ($nhnum + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64; $tmp .= $chars[$j]; } $tmplen = strlen($tmp); $tmp = substr_replace($tmp, $ch3, $nh2 % ++$tmplen, 0); $tmp = substr_replace($tmp, $ch2, $nh1 % ++$tmplen, 0); $tmp = substr_replace($tmp, $ch1, $knum % ++$tmplen, 0); return $tmp; } /**
- 解密函数 *
- @param string $txt 需要解密的字符串
- @param string $key 密匙
- @return string 返回解密结果 */ function decryption(string $txt=’’, string $key = ‘’, int $ttl = 0) { if (empty($txt)) return $txt; if (empty($key)) $key = md5(‘AUTH_CODE’); $chars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.”; $ikey = “-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm”; $knum = 0; $i = 0; $tlen = @strlen($txt); while (isset($key[$i])) $knum += ord($key[$i++]); $ch1 = @$txt[$knum % $tlen]; $nh1 = strpos($chars, $ch1); $txt = @substr_replace($txt, ‘’, $knum % $tlen–, 1); $ch2 = @$txt[$nh1 % $tlen]; $nh2 = @strpos($chars, $ch2); $txt = @substr_replace($txt, ‘’, $nh1 % $tlen–, 1); $ch3 = @$txt[$nh2 % $tlen]; $nh3 = @strpos($chars, $ch3); $txt = @substr_replace($txt, ‘’, $nh2 % $tlen–, 1); $nhnum = $nh1 + $nh2 + $nh3; $mdKey = substr(md5(md5(md5($key . $ch1) . $ch2 . $ikey) . $ch3), $nhnum % 8, $knum % 8 + 16); $tmp = ‘’; $j = 0; $k = 0; $tlen = @strlen($txt); $klen = @strlen($mdKey); for ($i = 0; $i < $tlen; $i++) { $k = $k == $klen ? 0 : $k; $j = strpos($chars, $txt[$i]) - $nhnum - ord($mdKey[$k++]); while ($j < 0) $j += 64; $tmp .= $chars[$j]; } $tmp = str_replace(array(‘-‘, ‘’, ‘.’), array(‘+’, ‘/’, ‘=’), $tmp); $tmp = trim(base64decode($tmp)); if (preg_match(“/\d{10}/s”, substr($tmp, 0, 11))) { if ($ttl > 0 && (time() - substr($tmp, 0, 11) > $ttl)) { $tmp = null; } else { $tmp = substr($tmp, 11); } } return $tmp; } ```
- 判断一个字符串是否属于序列化后的数据
```
/**
- 判断一个字符串是否属于序列化后的数据
- @param data 数据 author 新 */ function is_serialized($data) { $data = trim($data); if (‘N;’ == $data) return true; if (!preg_match(‘/^([adObis]):/’, $data, $badions)) return false; switch ($badions[1]) { case ‘a’: case ‘O’: case ‘s’: if (preg_match(“/^{$badions[1]}:[0-9]+:.[;}]$/s”, $data)) return true; break; case ‘b’: case ‘i’: case ‘d’: if (preg_match(“/^{$badions[1]}:[0-9.E-]+;$/”, $data)) return true; break; } return false; } ```
- 对象转数组
```
/**
- 对象转数组
- author 新 */ function object_array($array) { if (is_object($array)) { $array = (array)$array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = object_array($value); } } return $array; } ```
- 对字符串执行指定次数替换
```
/**
- 对字符串执行指定次数替换
- @param Mixed $search 查找目标值
- @param Mixed $replace 替换值
- @param Mixed $subject 执行替换的字符串/数组
- @param Int $limit 允许替换的次数,默认为-1,不限次数
- @return Mixed
*/
function str_replace_limit($search, $replace, $subject, $limit = -1)
{
if (is_array($search)) {
foreach ($search as $k => $v) {
$search[$k] = ‘
' . preg_quote($search[$k], '’) . ‘'; } } else { $search = '’ . preg_quote($search, ‘') . '’; } return preg_replace($search, $replace, $subject, $limit); } ```
- 读取目录列表
```
/**
- 读取目录列表
- 不包括 . .. 文件 三部分 *
- @param string $path 路径
- @return array 数组格式的返回结果 */ function readDirList($path) { if (is_dir($path)) { $handle = @opendir($path); $dir_list = array(); if ($handle) { while (false !== ($dir = readdir($handle))) { if ($dir != ‘.’ && $dir != ‘..’ && $dir != ‘.svn’ && is_dir($path . DIRECTORY_SEPARATOR . $dir)) { $dir_list[] = $dir; } } return $dir_list; } else { return false; } } else { return false; } } ```
- 过滤数组元素前后空格
```
/**
- 过滤数组元素前后空格 (支持多维数组)
- @param $array 要过滤的数组
- @return array|string */ function trim_array_element($array) { if (!is_array($array)){ return trim($array); } return array_map(‘trim_array_element’, $array); } ```
- 过滤二维数组重复值
```
/**
- 过滤二维数组
- @param $array 要过滤的数组 */ function multi_unique($array) { foreach ($array as $k => $na){ $new[$k] = serialize($na); } $uniq = array_unique($new); foreach ($uniq as $k => $ser){ $new1[$k] = unserialize($ser); } return ($new1); } ```
- 获取url中的各个参数
```
/**
- 获取url中的各个参数
- 类似于 pay_code=alipay&bank_code=ICBC-DEBIT
- @param type $str
- @return type */ function parse_url_param($str) { $data = array(); $str = explode(‘?’, $str); $str = end($str); $parameter = explode(‘&’, $str); foreach ($parameter as $val) { $tmp = explode(‘=’, $val); $data[$tmp[0]] = $tmp[1]; } return $data; } ```