• php一些实用的自制方法


    时间戳转多久之前

    function mdate($time = NULL) {
        $text = '';
        $time = $time === NULL || $time > time() ? time() : intval($time);
        $t = time() - $time; //时间差 (秒)
        $y = date('Y', $time)-date('Y', time());//是否跨年
        switch($t){
         case $t == 0:
           $text = '刚刚';
           break;
         case $t < 60:
          $text = $t . '秒前'; // 一分钟内
          break;
         case $t < 60 * 60:
          $text = floor($t / 60) . '分钟前'; //一小时内
          break;
         case $t < 60 * 60 * 24:
          $text = floor($t / (60 * 60)) . '小时前'; // 一天内
          break;
         case $t < 60 * 60 * 24 * 3:
          $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
          break;
         case $t < 60 * 60 * 24 * 30:
          $text = date('m月d日 H:i', $time); //一个月内
          break;
         case $t < 60 * 60 * 24 * 365&&$y==0:
          $text = date('m月d日', $time); //一年内
          break;
         default:
          $text = date('Y年m月d日', $time); //一年以前
          break;
        }
            
        return $text;
    }

    //json乱码转中文

    function decodeUnicode($str){
      return preg_replace_callback('/\\u([0-9a-f]{4})/i',
        create_function(
          '$matches',
          'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
        ),
        $str);
    }
    //时间戳转日期
    function MyDate($time){
        return date("Y-m-d H:i:s",$time);
    }
    //获取真实ip
    function getClientIP(){  
        global $ip;  
        if(getenv("HTTP_CLIENT_IP")){
            $ip = getenv("HTTP_CLIENT_IP");  
        }else if(getenv("HTTP_X_FORWARDED_FOR")){
            $ip = getenv("HTTP_X_FORWARDED_FOR");  
        }else if(getenv("REMOTE_ADDR")){
            $ip = getenv("REMOTE_ADDR");  
        }else{
            $ip = "NULL";  
        }
        return $ip;  
    }

    curl爬虫

    function _grab($curl,$postInfo='',$cookie='',$referer='',$userAgent=''){
         $ch = curl_init();  
         curl_setopt($ch, CURLOPT_URL, $curl);  
         //不输出头
         curl_setopt($ch, CURLOPT_HEADER, 0);   
         //以字符串返回获取的信息,不直接输出
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         //如果是https链接,不验证证书
         if(preg_match('/https/i', $curl)){
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         }
         //POST
         if($postInfo){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$postInfo);
         }
         //加入cookie
         if($cookie){
             curl_setopt($ch,CURLOPT_COOKIE,$cookie);
         }
         //模拟来路
         if($referer){
             curl_setopt($ch, CURLOPT_REFERER, $referer);
         }
         //模拟环境
         if($userAgent){
             curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
         }
         //执行
         $content = curl_exec($ch);  
         //错误处理
         if ($content  === false) {  
           return "网络请求出错: " . curl_error($ch);  
           exit();  
         }  
       curl_close($ch);
    return $content; }
       //输入二维数组,输出二维数组的全部可能,用于生成sku
        //$data = ['颜色'=>['红','黄','蓝'],'内存'=>['4G','8G']];
        //得到:Array ( [0] => Array ( [颜色] => 红 [内存] => 4G ) [1] => Array ( [颜色] => 红 [内存] => 8G ) [2] => Array ( [颜色] => 黄 [内存] => 4G ) [3] => Array ( [颜色] => 黄 [内存] => 8G ) [4] => Array ( [颜色] => 蓝 [内存] => 4G ) [5] => Array ( [颜色] => 蓝 [内存] => 8G ) )
        public function getSkus($data){
     /*       $data = ['颜色'=>['红','黄','蓝'],'内存'=>['4G','8G']];*/
            //用来装key
            $index = [];
            foreach ($data as $key => $value) {
                //key的集合
                $index[] = $key;
            }   
            
            //data数组元素的个数
            $count = count($index);
            //newData 用于装结果
            $newData = [];
    
            //第一种可能
            if($count==1){
                //此时   $index[0]就是$data的第一个key
                $data_1 = $data[$index[0]];    
                //如果只有一位,for循环只有一层
                for($x = 0;$x<count($data_1);$x++){
                    //$index[0]是索引
                    $newData[] = [$index[0]=>$data_1[$x]];
                }    
            }
    
            elseif($count==2){
                $data_1 = $data[$index[0]];    
                $data_2 = $data[$index[1]];    
                for($x = 0; $x<count($data_1);$x++){
                    for($y = 0; $y <count($data_2); $y++){
                        //index[0] , 和 ,$index[1]分别是索引
                        $newData[] = [$index[0] => $data_1[$x],$index[1] => $data_2[$y]];
                    }
                }
            }
    
            elseif($count==3){
                $data_1 = $data[$index[0]];    
                $data_2 = $data[$index[1]];
                $data_3 = $data[$index[2]];        
                for($x = 0; $x<count($data_1);$x++){
                    for($y = 0; $y <count($data_2); $y++){
                        for($z = 0; $z <count($data_3) ; $z++){
                            //index[0] , 和 ,$index[1] ,$index[2]  分别是索引
                            $newData[] = [$index[0] => $data_1[$x],$index[1] => $data_2[$y],$index[2] => $data_3[$z]];
                        }                
                    }
                }
            }
    
            elseif($count==4){
                $data_1 = $data[$index[0]];    
                $data_2 = $data[$index[1]];
                $data_3 = $data[$index[2]];    
                $data_4 = $data[$index[3]];        
                for($x = 0; $x<count($data_1);$x++){
                    for($y = 0; $y <count($data_2); $y++){
                        for($z = 0; $z <count($data_3) ; $z++){
                            for($a = 0; $a <count($data_4) ; $a++){
                                 //index[0] , 和 ,$index[1] ,$index[2]  分别是索引
                                $newData[] = [$index[0] => $data_1[$x],$index[1] => $data_2[$y],$index[2] => $data_3[$z],$index[3] => $data_4[$a]];                           
                            }
                        }                
                    }
                }
            }
    
            else{
                return false;
            }
            return $newData; 
        }
    //写入日志date('m_d')
    function my_log($data){
       $log_file = date('m_txt__d__',time()).'.txt';
      /* $content =var_export($data,TRUE);*/
       $data .= "
    ";
       file_put_contents($log_file,$data, FILE_APPEND);
    }
    //从txt一行一行读取成数组
    function readKeyword(){
    
        $data = [];
        $f= fopen("danci.txt","r");
        $count = 0;
        while (!feof($f))
        {
          $line = fgets($f);
          $line = substr($line, 0,6);
          $data[] = $line;
        }
        fclose($f);
        return $data;
    }
    编码
    
    header("Content-Type:text/html;charset=utf-8");
    
    //时区
    date_default_timezone_set("PRC");
    
    
    ignore_user_abort(); //关掉浏览器,PHP脚本也可以继续执行.
    set_time_limit(0); // 通过set_time_limit(0)可以让程序无限制的执行下去
    
    
    ob_flush(); //输出缓冲
    flush(); 
         //网络图片转换本地图片
        function jpgChangeLocal($pic){
    
            //获取文件
            $file = _request($pic);     
            $end = rand(1000,9999).'_'.time().'.jpg';
            $dir_path = 'd:/image';
            if(!is_dir($dir_path)){
                mkdir($dir_path);
            }
    
            $filename = $dir_path.'/'.$end;
            //打开写入流
            $resource = fopen($filename, 'a');
            //写入
            fwrite($resource, $file);
            //关闭资源
            fclose($resource);
        
            return '@'.dirname(__FILE__).'/'.$filename;
        }
  • 相关阅读:
    unittest模块小结
    自定义日志类
    配置文件的简单操作
    自动化框架搭建(一)
    openpyxl一点心得
    关于过滤字符中不同元素小结
    关于传入列表出现元素次数排序题目的一点心得
    Python中*和**的作用(课堂小结)
    Python代码的人机大战(循环嵌套)
    最后期限阅读笔记02
  • 原文地址:https://www.cnblogs.com/cl94/p/9606524.html
Copyright © 2020-2023  润新知