设为首页 收藏本站
查看: 737|回复: 0

[经验分享] php常用函数整理

[复制链接]

尚未签到

发表于 2018-12-12 06:10:08 | 显示全部楼层 |阅读模式
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
*   当$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[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[&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[0].&quot; 当月的最后一天: &quot;.$day[1];   
        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(
        [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;
}



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-650232-1-1.html 上篇帖子: php序列化函数漏洞 下篇帖子: php优化与模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表