温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中常用的函数总结

发布时间:2021-08-11 22:55:50 阅读:111 作者:chen 栏目:web开发
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容主要讲解“php中常用的函数总结”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php中常用的函数总结”吧!

1、字符串编码转换

/**
 * 字符串编码转换
 * 
 * @param  string  $str          待处理的字符
 * @param  string  $in_charset   输入编码
 * @param  string  $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、数组编码转换

/**
 * 数组编码转换
 * 
 * @param  array   $arr          待处理的数组
 * @param  string  $in_charset   输入编码
 * @param  string  $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、从内容中匹配出图片信息

/**
 * 从内容中匹配出图片信息(有多少图片信息就匹配出多少)
 *
 * @param  string   $content         内容信息
 * @param  boolean  $b_only_img_url  是否只获取图片地址,默认为true
 * @return array
 *   <li>当$b_only_img_url = true时,只返回图片地址的一维数组</li>
 *   <li>当$b_only_img_url = false时,返回图片地址的多种信息,二维数组,如下:</li>
 *   <li>img_tag  =>  '<img src="https://cache.yisu.com/upload/information/20200310/52/106968.jpg" />'</li>
 *   <li>img_src  =>  'src="https://cache.yisu.com/upload/information/20200310/52/106968.jpg"'</li>
 *   <li>img_url  =>  'https://cache.yisu.com/upload/information/20200310/52/106968.jpg'</li>
 */
 function get_img_list_from_content($content$b_only_img_url true){
  preg_match_all('/<img[^>]*?(?P<img_src_arr>src\s*=\s*([\'"]|&quot;|&#039;|&#39;)(?P<img_url_arr>.*?)([\'"]|&quot;|&#039;|&#39;))[^>]*?>/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编码
 *
 * @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 返回今天,周末,下周,未来,过去,某个时间段对应的时间戳
 * @param  string   $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["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 时间戳
 * @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 "<br/>";   
        echo date('Y-m-t'strtotime('-1 month'));   
        echo "<br/>";
    2.获取当月第一天及最后一天.
        $BeginDate=date('Y-m-01'strtotime(date("Y-m-d")));   
        echo $BeginDate;   
        echo "<br/>";   
        echo date('Y-m-d'strtotime("$BeginDate +1 month -1 day"));   
        echo "<br/>";
    3.获取当天年份、月份、日及天数.   
        echo " 本月共有:".date("t")."天";   
        echo " 当前年份".date('Y');   
        echo " 当前月份".date('m');   
        echo " 当前几号".date('d');   
        echo "<br/>";
    4.使用函数及数组来获取当月第一天及最后一天,比较实用   
        function getthemonth($date)
        {
            $firstday date('Y-m-01'strtotime($date));
            $lastday date('Y-m-d'strtotime("$firstday +1 month -1 day"));   return array($firstday,$lastday);
        }
        $today date("Y-m-d");
        $day=getthemonth($today);   
        echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];   
        echo "<br/>";
    5.封装了一个方法,开箱即用:
        $year 2017;
        $month 2; 
        function get_month_first_and_last_day($year$month)
        {
            if(empty($year) || empty($month)) {   
                return array();
            }

            $date $year "-" . $month;
    
            $begin_date date('Y-m-01 00:00:00'strtotime($date));
    
            $last_date  date('Y-m-d 23:59:59'strtotime("$begin_date +1 month -1 day"));        
            return array('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-01 00:00:00
        [last_date] => 2017-02-28 23:59:59)

8、根据二维数组的数据字段名返回其对应的值数组

* 根据二维数组的数据字段名返回其对应的值数组
 * 
 * @param  array    $rows         二维数组
 * @param  string   $field_name   字段名
 * @param  boolean  $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;
}

到此,相信大家对“php中常用的函数总结”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×