ts2009 发表于 2018-12-12 06:10:08

php常用函数整理

1、字符串编码转换
/**
* 字符串编码转换
*
* @paramstring$str          待处理的字符
* @paramstring$in_charset   输入编码
* @paramstring$out_charset输出编码
* @return string
*/
function str_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输出编码
* @return array
*/
function arr_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
* @return array
*   当$b_only_img_url = true时,只返回图片地址的一维数组
*   当$b_only_img_url = false时,返回图片地址的多种信息,二维数组,如下:
*   img_tag=>''
*   img_src=>'src="http://www.baidu.com/img/bdlogo.gif"'
*   img_url=>'http://www.baidu.com/img/bdlogo.gif'
*/
function get_img_list_from_content($content, $b_only_img_url = true){
preg_match_all('/]*?(?Psrc\s*=\s*([\'"]|"|'|')(?P.*?)([\'"]|"|'|'))[^>]*?>/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[$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编码
*
* @param string $str 字符串
* @return string
*/
function make_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));   
    return bcmul($crc1,$crc2);
}5、根据某天返回特定日期
/**
* 根据当天的时间,返回此周末的时间
*
* @param string $date
* @return array $date_arr($Sat,$Sun)
*/
function get_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);
    }   
    return array("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时,需要传入时间戳
* @return array;
*/
function get_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[&quot;end_time&quot;] = &quot;< {$nowtime}&quot;;            
            break;      
      case &quot;time_to_time&quot;: //选择时间段
            $end_time = strtotime($search_end_time);            
            if(!empty($end_time)) {
                  $day   = strtotime(date('Y-m-d',$end_time));
                  $tomorrow = $day+86400;
                  $querys[&quot;start_time&quot;] = $tomorrow;
                  $querys[&quot;end_time&quot;] = $day;
                }            
            break;      
         default:
            break;
    }   
    return $querys;
}6、根据过期时间判断剩余的天数
/**
* 根据过期时间判断剩余的天数
* @desc   如果为0,则表示活动已经结束
* @param$expire_time 时间戳
* @return float|int
*/
function check_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.获取上个月第一天及最后一天.   
      echo date('Y-m-01', strtotime('-1 month'));   
      echo &quot;&quot;;   
      echo date('Y-m-t', strtotime('-1 month'));   
      echo &quot;&quot;;
    2.获取当月第一天及最后一天.
      $BeginDate=date('Y-m-01', strtotime(date(&quot;Y-m-d&quot;)));   
      echo $BeginDate;   
      echo &quot;&quot;;   
      echo date('Y-m-d', strtotime(&quot;$BeginDate +1 month -1 day&quot;));   
      echo &quot;&quot;;
    3.获取当天年份、月份、日及天数.   
      echo &quot; 本月共有:&quot;.date(&quot;t&quot;).&quot;天&quot;;   
      echo &quot; 当前年份&quot;.date('Y');   
      echo &quot; 当前月份&quot;.date('m');   
      echo &quot; 当前几号&quot;.date('d');   
      echo &quot;&quot;;
    4.使用函数及数组来获取当月第一天及最后一天,比较实用   
      function getthemonth($date)
      {
            $firstday = date('Y-m-01', strtotime($date));
            $lastday = date('Y-m-d', strtotime(&quot;$firstday +1 month -1 day&quot;));   return array($firstday,$lastday);
      }
      $today = date(&quot;Y-m-d&quot;);
      $day=getthemonth($today);   
      echo &quot;当月的第一天: &quot;.$day.&quot; 当月的最后一天: &quot;.$day;   
      echo &quot;&quot;;
    5.封装了一个方法,开箱即用:
      $year = 2017;
      $month = 2;
      function get_month_first_and_last_day($year, $month)
      {
            if(empty($year) || empty($month)) {   
                return array();
            }
            $date = $year . &quot;-&quot; . $month;
            $begin_date = date('Y-m-01 00:00:00', strtotime($date));
            $last_date= date('Y-m-d 23:59:59', strtotime(&quot;$begin_date +1 month -1 day&quot;));      
            return array('begin_date' => $begin_date, 'last_date' => $last_date);
      }
      $ret = get_month_first_and_last_day($year, $month);
      print_r($ret);Array(
       => 2017-02-01 00:00:00
       => 2017-02-28 23:59:59)8、根据二维数组的数据字段名返回其对应的值数组
* 根据二维数组的数据字段名返回其对应的值数组
*
* @paramarray    $rows         二维数组
* @paramstring   $field_name   字段名
* @paramboolean$b_off_empty是否排除空值,默认:true
* @return array
*/
function array_values_by_field_name($rows, $field_name, $b_off_empty = false){
    $ret = array();
    foreach($rows as $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;
}


页: [1]
查看完整版本: php常用函数整理