/***10进制转16进制处理*@paramint$dev10进制数值*@paramint$length*@returnstring$hex16进制数值*/functionDevToHex($dev,$length=2){$hex=str_pad(dechex($dev),$length,'0',STR_PAD_LEFT);return$hex;}
$temp=[['id'=>1,'name'=>'name1','age'=>'age1','time'=>'time1'],['id'=>2,'name'=>'name2','age'=>'age2','time'=>'time2']];#清空原数据$data=$temp;foreach($dataas&$value){$value=[];}echo'<pre>';print_r($data);#重置原数据$data=$temp;foreach($dataas&$value){$value=['hobby'=>1];}print_r($data);#追加原数据$data=$temp;foreach($dataas&$value){$value['hobby']=1;}print_r($data);
$array=['a'=>'我是第一个','b'=>'我是第二个','c'=>'我是第三个','f'=>null];if(isset($array['a'])){echo'a存在';}else{echo'a不存在';}if(array_key_exists('d',$array)){echo'd存在';}else{echo'd不存在';}if(empty($array['f'])){echo'f不存在';}else{echo'f存在,且不为null,0,"0",false';}//a存在d不存在f不存在
//calc(参数1参数2,参数3);//参数3指定运算方法:add加,sub减,mul乘,div除,pow幂,mod取模,sqrt求算术平方根//加减乖除:参数1加上/减去/乘以/除以参数2//幂:参数1的参数2次方.//模:参数1除以参数2得到的余数.//算术平方根:求参数1的算术平方根.参数2不起作用,但不能省略.//echocalc('111111111111111111111111111111111111111111111111111111111111111111111111111110','2','mul');functioncalc($m,$n,$x){$errors=array('被除数不能为零','负数没有平方根');switch($x){case'add':$t=bcadd($m,$n,2);break;case'sub':$t=bcsub($m,$n,2);break;case'mul':$t=bcmul($m,$n,2);break;case'div':if($n!=0){$t=bcdiv($m,$n,2);}else{return$errors[0];}break;case'pow':$t=bcpow($m,$n,2);break;case'mod':if($n!=0){$t=bcmod($m,$n,2);}else{return$errors[0];}break;case'sqrt':if($m>=0){$t=bcsqrt($m,2);}else{return$errors[1];}break;}//$t=preg_replace("/\..*0+$/",'',$t);return$t;}
/***curl_post*url请求地址*postdata传输的数据,数组格式*/functioncurl_post($url,$postdata){$header=array('Accept:application/json',);//初始化$curl=curl_init();//设置抓取的urlcurl_setopt($curl,CURLOPT_URL,$url);//设置头文件的信息作为数据流输出curl_setopt($curl,CURLOPT_HEADER,0);//设置获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//超时设置curl_setopt($curl,CURLOPT_TIMEOUT,10);//超时设置,以毫秒为单位//curl_setopt($curl,CURLOPT_TIMEOUT_MS,500);//设置请求头curl_setopt($curl,CURLOPT_HTTPHEADER,$header);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);//设置post方式提交curl_setopt($curl,CURLOPT_POST,1);curl_setopt($curl,CURLOPT_POSTFIELDS,$postdata);//执行命令$data=curl_exec($curl);if(curl_error($curl)){returnfalse;}else{//打印返回的内容curl_close($curl);return$data;}}
/***curl_get*url请求地址*/functioncurl_get($url){$header=array('Accept:application/json',);$curl=curl_init();//设置抓取的urlcurl_setopt($curl,CURLOPT_URL,$url);//设置头文件的信息作为数据流输出curl_setopt($curl,CURLOPT_HEADER,0);//超时设置,以秒为单位curl_setopt($curl,CURLOPT_TIMEOUT,1);//超时设置,以毫秒为单位//curl_setopt($curl,CURLOPT_TIMEOUT_MS,500);//设置请求头curl_setopt($curl,CURLOPT_HTTPHEADER,$header);//设置获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);//执行命令$data=curl_exec($curl);if(curl_error($curl)){returnfalse;}else{//打印返回的内容curl_close($curl);return$data;}}
/***转换字符编码为utf-8*/functionstrToUtf8($str){$encode=mb_detect_encoding($str,array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));if($encode=='UTF-8'){return$str;}else{returnmb_convert_encoding($str,'UTF-8',$encode);}}
/***PHP正则替换markdown代码中的所有图片链接地址*http_path添加的图片路径**/functionhandleDescriptionMackdown($description,$http_path){$pregRule='/!\\[.*\\]\\((.+)\\)/';$description=preg_replace($pregRule,'![]('.$http_path.'${1})',$description);return$description;}
/***PHP正则替换HTML代码中的所有图片链接地址*descriptionhtmlspecialchars()后的字符串*http_path添加的图片路径**/functionhandleDescriptionHtml($description,$http_path){$description=htmlspecialchars_decode($description);$pregRule='/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';$description=preg_replace($pregRule,'<imgsrc="'.$http_path.'${1}"/>',$description);return$description;}