php中常用的函數(shù)總結(jié)

本篇內(nèi)容主要講解“php中常用的函數(shù)總結(jié)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php中常用的函數(shù)總結(jié)”吧!

成都創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、成都全網(wǎng)營銷推廣、微信小程序開發(fā)、微信公眾號(hào)開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計(jì)、程序開發(fā)來完成項(xiàng)目落地,為成都三輪攪拌車企業(yè)提供源源不斷的流量和訂單咨詢。

1、字符串編碼轉(zhuǎn)換

/**
 * 字符串編碼轉(zhuǎn)換
 * 
 * @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、數(shù)組編碼轉(zhuǎn)換

/**
 * 數(shù)組編碼轉(zhuǎn)換
 * 
 * @param  array   $arr          待處理的數(shù)組
 * @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、從內(nèi)容中匹配出圖片信息

/**
 * 從內(nèi)容中匹配出圖片信息(有多少圖片信息就匹配出多少)
 *
 * @param  string   $content         內(nèi)容信息
 * @param  boolean  $b_only_img_url  是否只獲取圖片地址,默認(rèn)為true
 * @return array
 *   <li>當(dāng)$b_only_img_url = true時(shí),只返回圖片地址的一維數(shù)組</li>
 *   <li>當(dāng)$b_only_img_url = false時(shí),返回圖片地址的多種信息,二維數(shù)組,如下:</li>
 *   <li>img_tag  =>  '<img src="/upload/otherpic44/106968.jpg" />'</li>
 *   <li>img_src  =>  'src="/upload/otherpic44/106968.jpg"'</li>
 *   <li>img_url  =>  '/upload/otherpic44/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、獲取一個(gè)Hash編碼

/**
 * 獲取一個(gè)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、根據(jù)某天返回特定日期

/**
 * 根據(jù)當(dāng)天的時(shí)間,返回此周末的時(shí)間
 *
 * @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);
}

/*
 * 根據(jù)時(shí)間英文名稱,返回特定時(shí)間段戳
 * @desc 返回今天,周末,下周,未來,過去,某個(gè)時(shí)間段對應(yīng)的時(shí)間戳
 * @param  string   $time_type  時(shí)間形式(today, weekend, next_week,future_all,history, time_to_time)
 * @param $search_end_time  當(dāng)time_type為time_to_time時(shí),需要傳入時(shí)間戳
 * @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": //歷史活動(dòng)
            $nowtime=time();
            $querys["end_time"] = "< {$nowtime}";            
            break;        
        case "time_to_time": //選擇時(shí)間段
            $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、根據(jù)過期時(shí)間判斷剩余的天數(shù)

/**
 * 根據(jù)過期時(shí)間判斷剩余的天數(shù)
 * @desc   如果為0,則表示活動(dòng)已經(jīng)結(jié)束
 * @param  $expire_time 時(shí)間戳
 * @return float|int
 */
 function check_remaining_days($expire_time)
 {  
     // 獲取當(dāng)前時(shí)間
    $cur_time = time();
    $expire_time = (int)$expire_time;

    $diff_time = ($expire_time - $cur_time);
    $remaining_days_count = 0;    
    if($diff_time > 0) {        
        // 計(jì)算剩余的天數(shù)
        $remaining_days_count = ceil($diff_time / (24 * 3600));
    }    
    return $remaining_days_count;
}

7、獲取某月的第一天和最后一天

// php獲取當(dāng)月天數(shù)及當(dāng)月第一天及最后一天、上月第一天及最后一天實(shí)現(xiàn)方法
    1.獲取上個(gè)月第一天及最后一天.   
        echo date('Y-m-01', strtotime('-1 month'));   
        echo "<br/>";   
        echo date('Y-m-t', strtotime('-1 month'));   
        echo "<br/>";
    2.獲取當(dāng)月第一天及最后一天.
        $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.獲取當(dāng)天年份、月份、日及天數(shù).   
        echo " 本月共有:".date("t")."天";   
        echo " 當(dāng)前年份".date('Y');   
        echo " 當(dāng)前月份".date('m');   
        echo " 當(dāng)前幾號(hào)".date('d');   
        echo "<br/>";
    4.使用函數(shù)及數(shù)組來獲取當(dāng)月第一天及最后一天,比較實(shí)用   
        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 "當(dāng)月的第一天: ".$day[0]." 當(dāng)月的最后一天: ".$day[1];   
        echo "<br/>";
    5.封裝了一個(gè)方法,開箱即用:
        $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、根據(jù)二維數(shù)組的數(shù)據(jù)字段名返回其對應(yīng)的值數(shù)組

* 根據(jù)二維數(shù)組的數(shù)據(jù)字段名返回其對應(yīng)的值數(shù)組
 * 
 * @param  array    $rows         二維數(shù)組
 * @param  string   $field_name   字段名
 * @param  boolean  $b_off_empty  是否排除空值,默認(rèn):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中常用的函數(shù)總結(jié)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

當(dāng)前標(biāo)題:php中常用的函數(shù)總結(jié)
文章起源:http://bm7419.com/article40/igeoho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)建站公司、網(wǎng)站策劃、響應(yīng)式網(wǎng)站、App開發(fā)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)