functionArrToXml($arr){if(!is_array($arr)||count($arr)==0)return'';$xml="<xml>";foreach($arras$key=>$val){if(is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">";}else{$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";}}$xml.="</xml>";return$xml;}
functionpostXml($url,$xmldata){$url=$url;$postfields=$xmldata;$ch=curl_init();$headers=[//"Content-Type:text/html;charset=UTF-8","Connection:Keep-Alive"];//$params[CURLOPT_HTTPHEADER]=$headers;//自定义header$params[CURLOPT_URL]=$url;//请求url地址$params[CURLOPT_HEADER]=false;//是否返回响应头信息$params[CURLOPT_RETURNTRANSFER]=true;//是否将结果返回$params[CURLOPT_FOLLOWLOCATION]=true;//是否重定向$params[CURLOPT_POST]=true;$params[CURLOPT_POSTFIELDS]=$postfields;$params[CURLOPT_SSL_VERIFYPEER]=false;$params[CURLOPT_SSL_VERIFYHOST]=false;curl_setopt_array($ch,$params);//传入curl参数$content=curl_exec($ch);//执行curl_close($ch);//关闭连接return$content;//返回输出登录结果}
functionhttp_curl($url,$type='get',$res='json',$arr=''){/*$url请求的url$type请求类型$res返回数据类型$arrpost请求参数*///1.初始化curl$ch=curl_init();//2.设置curl的参数curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);if($type=='post'){curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);}//3.采集$output=curl_exec($ch);//4.关闭curl_close($ch);if($res=='json'){returnjson_decode($output,true);}elseif(curl_errno($ch)){dmp(curl_errno($ch));}}
/***unicode转中文*@paramstring$nameunicode*@returnstring*/functionunicodeDecode($name){//转换编码,将Unicode编码转换成可以浏览的utf-8编码$pattern='/([\w]+)|(\\\u([\w]{4}))/i';preg_match_all($pattern,$name,$matches);if(!empty($matches)){$name='';for($j=0;$j<count($matches[0]);$j++){$str=$matches[0][$j];if(strpos($str,'\\u')===0){$code=base_convert(substr($str,2,2),16,10);$code2=base_convert(substr($str,4),16,10);$c=chr($code).chr($code2);$c=iconv('UCS-2','UTF-8',$c);$name.=$c;}else{$name.=$str;}}}return$name;}
functionmoreArrayUnique($arr,$field=[],$allField=true){//必须是传入数组if(!is_array($field))returnfalse;//先把二维数组中的内层数组的键值记录在在一维数组中foreach($arr[0]as$k=>$v)$allField[]=$k;if($field){//按照顺序排字段名foreach($allFieldas$key=>$val){if(!in_array($val,$field))unset($allField[$key]);}$field=$allField;}foreach($arras$k=>$v){foreach($vas$key=>$item){if(!in_array($key,$field))unset($v[$key]);}//降维用implode()也行$v=implode(",",$v);//保留原来的键值$temp[]即为不保留原来键值$temp[$k]=$v;}//去重:去掉重复的元素$arr=array_unique($temp);if($allField){foreach($arras$k=>$v){//拆分后的重组如:Array([0]=>张三[1]=>18)$a=explode(",",$v);//将原来的键与值重新合并$arrAfter[]=array_combine($field,$a);}}else{//将键值保存为一维数组$allKey=array_keys($temp);$arrAfter=[];foreach($arras$k=>$v){//数组去重if(!in_array($k,$allKey)){unset($arr[$k]);}else{$a=explode(",",$v);$arrAfter[]=array_combine($field,$a);}}}return$arrAfter??false;}
/***@param$image_pathstring图片路径*@parambool$is_full是否加上图片前缀*@returnstring*/functionbase64EncodeImage($image_path,$is_full=true){$base64_image='';$image_info=getimagesize($image_path);$image_data=fread(fopen($image_path,'r'),filesize($image_path));if($is_full){//data:image/jpg/png/gif;base64,$base64_image='data:'.$image_info['mime'].';base64,'.base64_encode($image_data);}else{$base64_image=base64_encode($image_data);}return$base64_image;}
functionnumToWord(int$num){$chiNum=array('零','一','二','三','四','五','六','七','八','九');$chiUni=array('','十','百','千','万','亿','十','百','千');$num_str=(string)$num;$count=strlen($num_str);$last_flag=true;//上一个是否为0$zero_flag=true;//是否第一个$temp_num=null;//临时数字$chiStr='';//拼接结果if($count==2){//两位数$temp_num=$num_str[0];$chiStr=$temp_num==1?$chiUni[1]:$chiNum[$temp_num].$chiUni[1];$temp_num=$num_str[1];$chiStr.=$temp_num==0?'':$chiNum[$temp_num];}elseif($count>2){$index=0;for($i=$count-1;$i>=0;$i--){$temp_num=$num_str[$i];if($temp_num==0){if(!$zero_flag&&!$last_flag){$chiStr=$chiNum[$temp_num].$chiStr;$last_flag=true;}}else{$chiStr=$chiNum[$temp_num].$chiUni[$index%9].$chiStr;$zero_flag=false;$last_flag=false;}$index++;}}else{$chiStr=$chiNum[$num_str[0]];}return$chiStr;}
functionhidePhone($str,int$start=3,int$length=4){//获取最后一位$end=$start+$length;//判断传参是否正确if($start<0||$end>11)returnfalse;$replace='';//用于判断多少for($i=0;$i<$length;$i++)$replace.='*';returnsubstr_replace($str,$replace,$start,$length);}
functiongetRandomStr($length=6){$chars='123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';$hash='';$max=strlen($chars)-1;for($i=0;$i<$length;$i++)$hash.=$chars[mt_rand(0,$max)];return$hash;}
functiontimeToBefore(int$time){$t=time()-$time;$f=array('31536000'=>'年','2592000'=>'个月','604800'=>'星期','86400'=>'天','3600'=>'小时','60'=>'分钟','1'=>'秒');foreach($fas$k=>$v){if(0!=$c=floor($t/(int)$k)){return$c.$v.'前';}}}