将 xml数据转换为数组格式
function xml_to_array($xml)
{
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if (preg_match_all($reg, $xml, $matches)) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i++) {
$subxml = $matches[2][$i];
$key = $matches[1][$i];
if (preg_match($reg, $subxml)) {
$arr[$key] = xml_to_array($subxml);
} else {
$arr[$key] = $subxml;
}
}
}
return $arr;
}
random() 函数返回随机整数
function random($length = 6, $numeric = 0)
{
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if ($numeric) {
$hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
/**
* 微信信息解密
* @param string $appid 小程序id
* @param string $sessionKey 小程序密钥
* @param string $encryptedData 在小程序中获取的encryptedData
* @param string $iv 在小程序中获取的iv
* @return array 解密后的数组
*/
function decryptData($appid, $sessionKey, $encryptedData, $iv)
{
$OK = 0;
$IllegalAesKey = -41001;
$IllegalIv = -41002;
$IllegalBuffer = -41003;
$DecodeBase64Error = -41004;
if (strlen($sessionKey) != 24) {
return $IllegalAesKey;
}
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
return $IllegalIv;
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj = json_decode($result);
if ($dataObj == NULL) {
return $IllegalBuffer;
}
if ($dataObj->watermark->appid != $appid) {
return $DecodeBase64Error;
}
$data = json_decode($result, true);
return $data;
}
/**
* 判断是否在数组中
*/
function deep_in_array($value, $array)
{
foreach ($array as $item) {
if (!is_array($item)) {
if ($item == $value) {
return $item;
} else {
continue;
}
}
if (in_array($value, $item)) {
return $item;
} else if ($this->deep_in_array($value, $item)) {
return $item;
}
}
return false;
}
//自动识别编码并转换为UTF-8
function characet($data)
{
if (!empty($data)) {
$char = mb_detect_encoding($data, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));
if ($char != 'UTF-8') {
$data = mb_convert_encoding($data, 'utf-8', $char);
}
}
return $data;
}
//接口json返回
function showAPIJson($errorCode, $msg, $jData = [], $requestUrl, $code = "200")
{
// eroorCode:成功 返回0,失败返回1
$jsonArr = array(
'error_code' => $errorCode,
'msg' => $msg,
'data' => $jData,
'request_url' => $requestUrl
);
return json($jsonArr, $code);
}
//获取指定长度串码
function getRandChar($length)
{
$str = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol) - 1;
for ($i = 0;
$i < $length;
$i++) {
$str .= $strPol[rand(0, $max)];
}
return $str;
}
/*
*建码函数,使用方法:CreateCoupon ("id",code_length,repaircode_length)
*功能:生成带唯一标识的伪随机码
*$newid:int 唯一标识符
*$newcodelen:int 第一段码值长度
*$newdisrepairlen:int 第二段码值长度
*/
function CreateCoupon($newid, $newcodelen, $newdisrepairlen)
{
$codelen = $newcodelen;
$id = $newid;
$disrepairlen = $newdisrepairlen;
$charset0 = 'abcdABCDefghjkEFGHKmnpqMNPRSTrstuUVWXYZlvwxyz23456789';//第一段随机码字符集,数字1,0与字母I,l,O过于形似,字符集中排除了这些字符
$charset = 'AaBbCcDdEeFfGgHhKkMmNnPpRrSsTtUuVvWwXxYyZz';//补齐码值的字符集,ID为10进制,字符集中不能出现数字[0-9],否则最终的字符串可能出现重复(尽管前半段与后半段同时重复的概率很低)
$_len = strlen($charset0) - 1;//字符集最大序号
$coupon = "";//创建随机码变量
//创建一个随机码,每一位都随机一次
for ($i = 0; $i < $codelen; $i++) {
$coupon .= $charset0[mt_rand(0, $_len)];
}
$nid = DispRepair($id, $newdisrepairlen, $charset, "0");//调用补位函数补齐特征字符串,防止券码出现重复,非1为后补
$coupon .= $nid;//组装成完整的随机码
return $coupon;
}
/*
补位函数,使用方法:DispRepair('getstr',repaircode_length,'fillstr','type')
功能:补齐字符串长度
$gstr:原字符串
$newlen:新字符串长度
$fill:补位字符集,不能出现唯一标识中可能出现的字符
type:类型,1为前补,其他值为后补
*/
function DispRepair($gstr, $disrepairlen, $fillstr, $type)
{
$length = $disrepairlen - strlen($gstr);//需要补齐的字符串长度
if ($length < 1) {
return $gstr;
} else {
$newstr = "";//创建新字符串
//要补齐的字符串,每一位都随机一次
for ($i = 0; $i < $length; $i++) {
$newstr .= $fillstr[mt_rand(0, strlen($fillstr) - 1)];//组装新字符串
}
if ($type == 1) {
$gstr = $newstr . $gstr;//将新字符串填充到原字符串前方
} else {
$gstr .= $newstr;//将新字符串填充到原字符串后方
}
}
return $gstr;
}
//转化友好时间格式
function get_last_time($time = NULL)
{
$text = '';
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
$y = date('Y', $time) - date('Y', time());//是否跨年
switch ($t) {
case $t == 0:
$text = '刚刚';
break;
case $t < 60:
$text = $t . '秒前'; // 一分钟内
break;
case $t < 60 * 60:
$text = floor($t / 60) . '分钟前'; //一小时内
break;
case $t < 60 * 60 * 24:
$text = floor($t / (60 * 60)) . '小时前'; // 一天内
break;
case $t < 60 * 60 * 24 * 3:
$text = floor($time / (60 * 60 * 24)) == 1 ? '昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time); //昨天和前天
break;
case $t < 60 * 60 * 24 * 30:
$text = date('m月d日 H:i', $time); //一个月内
break;
case $t < 60 * 60 * 24 * 365 && $y == 0:
$text = date('m月d日', $time); //一年内
break;
default:
$text = date('Y年m月d日', $time); //一年以前
break;
}
return $text;
}
//创建目录
function create_dirs($path)
{
if (!is_dir($path)) {
$directory_path = "";
$directories = explode("/", $path);
array_pop($directories);
foreach ($directories as $directory) {
$directory_path .= $directory . "/";
if (!is_dir($directory_path)) {
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
/**
* 使用scandir 遍历目录
*
* @param $path
* @return array
*/
function getDir($path)
{
//判断目录是否为空
if (!file_exists($path)) {
return [];
}
$files = scandir($path);
$fileItem = [];
foreach ($files as $v) {
$newPath = $path . DIRECTORY_SEPARATOR . $v;
if (is_dir($newPath) && $v != '.' && $v != '..') {
$fileItem = array_merge($fileItem, getDir($newPath));
} else if (is_file($newPath)) {
$newPath = mb_convert_encoding($newPath, 'UTF-8', 'GBK');
$newPath = str_replace('\\', '/', $newPath);
$extension = pathinfo($newPath, PATHINFO_EXTENSION);
if ($extension != 'html') $fileItem[] = $newPath;
}
}
return $fileItem;
}
/**
* 使用glob 遍历
* @param $path
*/
function getDir2($path)
{
//判断目录是否为空
if (!file_exists($path)) {
return [];
}
$fileItem = [];
//切换如当前目录
chdir($path);
foreach (glob('*') as $v) {
$newPath = $path . DIRECTORY_SEPARATOR . $v;
if (is_dir($newPath)) {
$fileItem = array_merge($fileItem, getDir2($newPath));
} else if (is_file($newPath)) {
$fileItem[] = $newPath;
}
}
return $fileItem;
}
/**
* 使用opendir 函数递归
*/
function getDir3($path)
{
if (!file_exists($path)) {
return [];
}
$handle = opendir($path);
$fileItem = [];
if ($handle) {
while (($file = readdir($handle)) !== false) {
$newPath = $path . DIRECTORY_SEPARATOR . $file;
if (is_dir($newPath) && $file != '.' && $file != '..') {
$fileItem = array_merge($fileItem, getDir3($newPath));
} else if (is_file($newPath)) {
$fileItem[] = $newPath;
}
}
}
@closedir($handle);
return $fileItem;
}
**
* dir 函数的方式,对象
*
* @param $path
* @return array
*/
function getDir4($path)
{
if (!file_exists($path)) {
return [];
}
$handel = dir($path);
$fileItem = [];
if ($handel) {
while (($file = $handel->read()) !== false) {
$newPath = $path . DIRECTORY_SEPARATOR . $file;
if (is_dir($newPath) && $file != '.' && $file != '..') {
$fileItem = array_merge($fileItem, getDir4($newPath));
} else if (is_file($newPath)) {
if (getFileType($file) == "jpg" || getFileType($file) == "png" || getFileType($file) == "gif") {
$str = '\\tpl\\';
$newPath = substr($newPath, strripos($newPath, $str) + 0);
$newPath = mb_convert_encoding($newPath, 'UTF-8', 'GBK');
// $newPath = url($newPath);
$newPath = str_replace('\\', '/', $newPath);
$newPath = str_replace('/home/wwwroot/ztrip.hnzhiu.com/public', '', $newPath);
$newPath = 'http://' . $_SERVER['HTTP_HOST'] . '/Tools/public/' . $newPath;
$fileItem[] = $newPath;
}
}
}
}
$handel->close();
return $fileItem;
}
/**
* 判断文件格式
* @param $path
* @return array
*/
function getFileType($filename)
{
return strtolower(pathinfo($filename)['extension']);
}
/**
* 解压文件到指定目录
* @param string zip压缩文件的路径
* @param string 解压文件的目的路径
* @param boolean 是否以压缩文件的名字创建目标文件夹
* @param boolean 是否重写已经存在的文件
* @return boolean 返回成功 或失败
*/
function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true)
{
if ($zip = zip_open($src_file)) {
if ($zip) {
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if ($dest_dir === false) {
$dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/";
}
// 如果不存在 创建目标解压目录
create_dirs($dest_dir);
// 对每个文件进行解压
while ($zip_entry = zip_read($zip)) {
// 文件不在根目录
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false) {
// 创建目录 在末尾带 /
create_dirs($dest_dir . substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1));
}
// 打开包
if (zip_entry_open($zip, $zip_entry, "r")) {
// 文件名保存在磁盘上
$file_name = $dest_dir . zip_entry_name($zip_entry);
// 检查文件是否需要重写
if ($overwrite === true || $overwrite === false && !is_file($file_name)) {
// 读取压缩文件的内容
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
@file_put_contents($file_name, $fstream);
// 设置权限
chmod($file_name, 0777);
echo "save: " . $file_name . "<br />";
}
// 关闭入口
zip_entry_close($zip_entry);
}
}
// 关闭压缩包
zip_close($zip);
}
} else {
return false;
}
return true;
}
//密码加盐
function getMd5Password($password)
{
$salt = config('security.salt');
return md5($password . $salt);
}