/***字符串编码转换**@paramstring$str待处理的字符*@paramstring$in_charset输入编码*@paramstring$out_charset输出编码*@returnstring*/functionstr_iconv($str,$in_charset='UTF-8',$out_charset='GBK'){$str=mb_convert_encoding($str,$out_charset,$in_charset);return$str;}
2、数组编码转换
/***数组编码转换**@paramarray$arr待处理的数组*@paramstring$in_charset输入编码*@paramstring$out_charset输出编码*@returnarray*/functionarr_iconv($arr,$in_charset='UTF-8',$out_charset='GBK'){$arr=eval('return'.mb_convert_encoding(var_export($arr,true),$out_charset,$in_charset).';');return$arr;}
3、从内容中匹配出图片信息
/***从内容中匹配出图片信息(有多少图片信息就匹配出多少)**@paramstring$content内容信息*@paramboolean$b_only_img_url是否只获取图片地址,默认为true*@returnarray*<li>当$b_only_img_url=true时,只返回图片地址的一维数组</li>*<li>当$b_only_img_url=false时,返回图片地址的多种信息,二维数组,如下:</li>*<li>img_tag=>'<imgsrc="http://www.baidu.com/img/bdlogo.gif"/>'</li>*<li>img_src=>'src="http://www.baidu.com/img/bdlogo.gif"'</li>*<li>img_url=>'http://www.baidu.com/img/bdlogo.gif'</li>*/functionget_img_list_from_content($content,$b_only_img_url=true){preg_match_all('/<img[^>]*?(?P<img_src_arr>src\s*=\s*([\'"]|"|'|')(?P<img_url_arr>.*?)([\'"]|"|'|'))[^>]*?>/msi',$content,$match);$arr_temp=array();if($match['img_url_arr']){foreach($match['img_url_arr']as$key=>$img_url){if($b_only_img_url){$img_info=$img_url;}else{$img_info=array('img_tag'=>$match[0][$key],'img_src'=>$match['img_src_arr'][$key],'img_url'=>$match['img_url_arr'][$key],);}$arr_temp[]=$img_info;}}return$arr_temp;}
4、获取一个Hash编码
/***获取一个Hash编码**@paramstring$str字符串*@returnstring*/functionmake_hash_code($str){if(empty($str))return'';$mdv=md5($str);$mdv1=substr($mdv,0,16);$mdv2=substr($mdv,16,16);$crc1=abs(crc32($mdv1));$crc2=abs(crc32($mdv2));returnbcmul($crc1,$crc2);}
5、根据某天返回特定日期
/***根据当天的时间,返回此周末的时间**@paramstring$date*@returnarray$date_arr($Sat,$Sun)*/functionget_weekend_by_date(){$year=date("Y");$month=date("m");$day=date("d");$nowday=mktime(0,0,0,$month,$day,$year);$w=(int)date("w",$nowday);if($w==0){$Sat=mktime(0,0,0,$month,$day-1,$year);$Sun=mktime(0,0,0,$month,$day+1,$year);}else{$t=6-$w;$Sat=mktime(0,0,0,$month,$day+$t,$year);$Sun=mktime(0,0,0,$month,$day+$t+2,$year);}returnarray("Sat"=>$Sat,"Sun"=>$Sun);}/**根据时间英文名称,返回特定时间段戳*@desc返回今天,周末,下周,未来,过去,某个时间段对应的时间戳*@paramstring$time_type时间形式(today,weekend,next_week,future_all,history,time_to_time)*@param$search_end_time当time_type为time_to_time时,需要传入时间戳*@returnarray;*/functionget_timestamp_by_time_type($time_type="today",$search_end_time=""){//支持的日期格式名称//$time_type_arr=array('today','weekend','next_week','future_arr','history','time_to_time');switch($time_type){case"today"://今天$today=strtotime(date('Y-m-d'));$tomorrow=$today+86400;$querys["start_time"]=$tomorrow;$querys["end_time"]=$today;break;case"weekend"://周末$arr=mforum_get_weekend_by_date();$querys["start_time"]=$arr["Sun"];$querys["end_time"]=$arr["Sat"];break;case"next_week"://未来7天$today=strtotime(date('Y-m-d'));$next_week=$today+(86400*7);$tomorrow=$today+86400;$querys["start_time"]=$next_week;$querys["end_time"]=$tomorrow;break;case"future_all"://未来全部$nowtime=time();$querys["end_time"]=$nowtime;break;case"history"://历史活动$nowtime=time();$querys["end_time"]="<{$nowtime}";break;case"time_to_time"://选择时间段$end_time=strtotime($search_end_time);if(!empty($end_time)){$day=strtotime(date('Y-m-d',$end_time));$tomorrow=$day+86400;$querys["start_time"]=$tomorrow;$querys["end_time"]=$day;}break;default:break;}return$querys;}
6、根据过期时间判断剩余的天数
/***根据过期时间判断剩余的天数*@desc如果为0,则表示活动已经结束*@param$expire_time时间戳*@returnfloat|int*/functioncheck_remaining_days($expire_time){//获取当前时间$cur_time=time();$expire_time=(int)$expire_time;$diff_time=($expire_time-$cur_time);$remaining_days_count=0;if($diff_time>0){//计算剩余的天数$remaining_days_count=ceil($diff_time/(24*3600));}return$remaining_days_count;}
7、获取某月的第一天和最后一天
//php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法1.获取上个月第一天及最后一天.echodate('Y-m-01',strtotime('-1month'));echo"<br/>";echodate('Y-m-t',strtotime('-1month'));echo"<br/>";2.获取当月第一天及最后一天.$BeginDate=date('Y-m-01',strtotime(date("Y-m-d")));echo$BeginDate;echo"<br/>";echodate('Y-m-d',strtotime("$BeginDate+1month-1day"));echo"<br/>";3.获取当天年份、月份、日及天数.echo"本月共有:".date("t")."天";echo"当前年份".date('Y');echo"当前月份".date('m');echo"当前几号".date('d');echo"<br/>";4.使用函数及数组来获取当月第一天及最后一天,比较实用functiongetthemonth($date){$firstday=date('Y-m-01',strtotime($date));$lastday=date('Y-m-d',strtotime("$firstday+1month-1day"));returnarray($firstday,$lastday);}$today=date("Y-m-d");$day=getthemonth($today);echo"当月的第一天:".$day[0]."当月的最后一天:".$day[1];echo"<br/>";5.封装了一个方法,开箱即用:$year=2017;$month=2;functionget_month_first_and_last_day($year,$month){if(empty($year)||empty($month)){returnarray();}$date=$year."-".$month;$begin_date=date('Y-m-0100:00:00',strtotime($date));$last_date=date('Y-m-d23:59:59',strtotime("$begin_date+1month-1day"));returnarray('begin_date'=>$begin_date,'last_date'=>$last_date);}$ret=get_month_first_and_last_day($year,$month);print_r($ret);Array([begin_date]=>2017-02-0100:00:00[last_date]=>2017-02-2823:59:59)
8、根据二维数组的数据字段名返回其对应的值数组
*根据二维数组的数据字段名返回其对应的值数组**@paramarray$rows二维数组*@paramstring$field_name字段名*@paramboolean$b_off_empty是否排除空值,默认:true*@returnarray*/functionarray_values_by_field_name($rows,$field_name,$b_off_empty=false){$ret=array();foreach($rowsas$row){if(isset($row[$field_name])){if($b_off_empty){if(!empty($row[$field_name])){$ret[]=$row[$field_name];}}else{$ret[]=$row[$field_name];}}}return$ret;}
php常用函数整理
原文地址:http://blog.51cto.com/13716819/2121561