php中将数组缓存化记录

    <?php  
      
    class Function_Class  
    {  
        function Function_Class()  
        {  
      
        }  
        /** 
     * 数组缓存字符串生成 
     * 
     * @param string $arrayName  要要缓存的数组名 
     * @param array $array   要缓存的数组� 
     * @return string 
     */  
        function export_ArrayString($arrayName,$array){  
      
            $var=Function_Class::export_ArrayString_child($array,1,"","");  
            $var="{1}quot;.$arrayName."=".$var.";";  
            return $var;  
        }  
      
        /** 
     * 数组缓存字符串生成 与export_ArrayString 配合 
     * 
     * @param array $array  要缓存的数组 
     * @param int $c 是否为第一次使用 
     * @param string $t  
     * @param string $var 上一次的数据 
     * @return string 
     */  
        function export_ArrayString_child($array,$c=1,$t='',$var=''){  
            $c && $var="array(\r\n";  
            $t.="  ";  
            if(is_array($array)){  
                foreach($array as $key => $value){  
                    $var.="$t'".addslashes($key)."'=>";  
                    if(is_array($value)){  
                        $var.="array(\r\n";  
                        $var=Function_Class::export_ArrayString_child($value,0,$t,$var);  
                        $var.="$t),\r\n";  
                    } else{  
                        $var.="'".addslashes($value)."',\r\n";  
                    }  
                }  
            }  
            if($c){  
                $var.=")";  
            }  
            return $var;  
        }  
      
        /** 
     *普通变量缓存字符串生成 
     * 
     * @param string $varName  变量名 
     * @param string $string  变量数据 
     * @return string 
     */  
        function export_StrString($varName,$string)  
        {  
            $string=addslashes($string);  
            $string="\"$string\";\r\n";  
            $string="{1}quot;.$varName."=".$string;  
            return $string;  
        }  
      
        /** 
         * 数据集缓存操作生成字符串 
         * 
         * @param obj $queryObj  数据集� 
         * @param string $arrayName 要缓存数组名� 
         * @param  int $result_type 数据集排序形式� 
         * @return string 
         */  
        function DataBase_QueryCache($queryObj,$arrayName,$result_type = MYSQL_ASSOC)  
        {  
            $blogdb=array();  
            while ($rt=mysql_fetch_array($queryObj,$result_type)) {  
                $blogdb[]=$rt;  
            }  
            unset($queryObj,$SelectSql);  
            return Function_Class::export_ArrayString($arrayName,$blogdb);  
        }  
      
        /** 
     * 把缓存字符串写入文件� 
     *  
     * @param string $filename  文件名 
     * @param string  $data 要写入的数据 
     * @param string $method 写入模式 与文件操作的模式一样 
     * @param string  $iflock  是否锁定文件 
     */  
        function writeFile($filename,$data,$method="rb+",$iflock=1){  
            touch($filename);  
            $handle=fopen($filename,$method);  
            if($iflock){  
                flock($handle,LOCK_EX);  
            }  
            fwrite($handle,$data);  
            if($method=="rb+") ftruncate($handle,strlen($data));  
            fclose($handle);  
        }  
      
        /** 
     * 读文件内容 
     * 
     * @param string $filename 文件名 
     * @param string $method  与件操作的模式 
     * @return string    
     */  
        function readFile($filename,$method="rb"){  
            if($handle=@fopen($filename,$method)){  
                flock($handle,LOCK_SH);  
                $filedata=fread($handle,filesize($filename));  
                fclose($handle);  
            }  
            return $filedata;  
        }  
      
        /** 
         * 数组排序方法 
         * 
         * @param  array $arraylist 要排序的数组 
         * @param string $key 要实行排序的字段名 
         * @param string $type  排序形式  desc   asc 
         * @return unknown 
         */  
        function mysort($arraylist=array(),$key="",$type="DESC")  
        {  
            if(!$arraylist)return $arraylist;  
            if(!$key)return $arraylist;  
            $tempArray=array();  
            foreach ($arraylist as $arrayKey=>$arrayValue)  
            {  
                foreach ($arraylist as $arrayKey2=>$arrayValue2)  
                {  
                    if(strtoupper($type)=="DESC")  
                    {  
                        if(intval($arrayValue[$key])>intval($arrayValue2[$key]))  
                        {  
                            $tempArray=$arrayValue;  
                            $arrayValue=$arrayValue2;  
                            $arrayValue2=$tempArray;  
                            $arraylist[$arrayKey]=$arrayValue;  
                            $arraylist[$arrayKey2]=$tempArray;  
                        }  
                    }  
                    else  
                    {  
                        if(intval($arrayValue[$key])<intval($arrayValue2[$key]))  
                        {  
                            $tempArray=$arrayValue;  
                            $arrayValue=$arrayValue2;  
                            $arrayValue2=$tempArray;  
                            $arraylist[$arrayKey]=$arrayValue;  
                            $arraylist[$arrayKey2]=$tempArray;  
                        }  
                    }  
                }  
                unset($tempArray);  
            }  
            return $arraylist;  
        }  
      
        /** 
         * 字符串节取 
         * 
         * @param string $str_cut 要节取的字符串 
         * @param int $length  要节取的长度 
         * @param bool $isshowmessage   是否显示message的变量值  
         * @param string $message    多出来显示的字符串 
         * @return string 
         */  
        function substr_cut($sourcestr,$cutlength=30,$isshowmessage=true,$message="..."){  
            $returnstr='';  
            $i=0;  
            $n=0;  
            $str_length=strlen($sourcestr);//字符串的字节数  
            while (($n<$cutlength) and ($i<=$str_length))  
            {  
                $temp_str=substr($sourcestr,$i,1);  
                $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码  
                if ($ascnum>=224) //如果ASCII位高与224,  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符  
                    $i=$i+3; //实际Byte计为3  
                    $n++; //字串长度计1  
                }  
                elseif ($ascnum>=192) //如果ASCII位高与192,  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符  
                    $i=$i+2; //实际Byte计为2  
                    $n++; //字串长度计1  
                }  
                elseif ($ascnum>=33 && $ascnum<=59) //其它标点号,  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数仍计1个  
                    $n++; //但考虑整体美观,大写字母计成一个高位字符  
                }  
                elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数仍计1个  
                    $n++; //但考虑整体美观,大写字母计成一个高位字符  
                }  
                elseif($ascnum>=48 && $ascnum<=57)//如果是数字的  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数仍计1个  
                    $n++; //但考虑整体美观,大写字母计成一个高位字符  
                }  
                elseif($ascnum===32)//如果是空格的  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数仍计1个  
                    $n++; //但考虑整体美观,大写字母计成一个高位字符  
                }  
                elseif($ascnum===64)//@  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数计1个  
                    $n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...  
                }  
                else //其他情况下,包括小写字母和半角标点符号,  
                {  
                    $returnstr=$returnstr.substr($sourcestr,$i,1);  
                    $i=$i+1; //实际的Byte数计1个  
                    $n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...  
                }  
            }  
            if ($str_length>$cutlength){  
                if($isshowmessage)  
                $returnstr = $returnstr . $message;//超过长度时在尾处加上省略号  
            }  
            return $returnstr;  
        }  
      
      
        /** 
         * 返回HTML内容 
         * 
         * @param string $content  要处理的非HTML内容 
         * @return string 
         */  
        function getHtmlString($content)  
        {  
            $Sw = $content;  
            if(empty($Sw))return "";  
            $Sw =str_replace("&","&",$Sw);  
            $Sw =str_replace("<","<",$Sw);  
            $Sw =str_replace(">",">",$Sw);  
            $Sw =str_replace(" "," ",$Sw);  
            $Sw =str_replace("\n","<br />",$Sw);  
            $Sw =str_replace("\"",""",$Sw);  
            $Sw =str_replace("'","’",$Sw);  
            $Sw =str_replace("\t","    ",$Sw);  
            $Sw =str_replace("?","&##8226;",$Sw);  
            return $Sw;  
        }  
      
        /** 
     *  
     *返回指时间内是否大于当前系统时差 暂只做月的 
     * @param timespace$cimTime 
     * @param int $comLength 
     * @param string $type 
     * @return bool 
     */  
        function timeOut($cimTime,$comLength=1,$type='M')  
        {  
            $type=strtoupper($type);  
            $thisSystemTime=time();//当前时间  
            $cimTime=intval($cimTime);  
            if($cimTime==$thisSystemTime)return true;//如果时间相同刚为通过状态  
            $dateArray=getdate($cimTime);//返回时间数组  
            if($type=="M")//是否大于指定月的  
            {  
                $monthDay=date("t",Function_Class::returnNextMonthDay($cimTime,$comLength));//返回当前时间月分的天数  
                $comthietime=$cimTime+($comLength*$monthDay*24*60*60);  
                if($comthietime<$thisSystemTime) return true; //过期  
                else  
                return false; //没有过期  
            }  
        }  
        /** 
     * 
     * @param 间间隔 $timespace 
     * @param 指时的时长 $comLength 
     * @return 时间间隔 
     */  
        function returnNextMonthDay($timespace,$comLength)  
        {  
            for ($i=1;$i<=$comLength;$i++)  
            {  
                $monthDay=date("t",$timespace);//返回当前时间月分的天数  
                $timespace=$timespace+($monthDay*24*60*60);//得到指定时间内加一这本月实际天数,既当前月再加一个月,如果$comLength>2以上则拿加后下一个月的天数再加一个月  
            }  
            return $timespace;  
        }  
      
        /** 
         * 返回指定内容,对应数据关键字段用指定符号分开 
         * 
         * @param obj $SpiltObj  数据 
         * @param bool $isquest  是否为数据集  不是则为数组 
         * @param string  $splitKey 要得到键名的 
         * @param string $isaddActionString  开头是否加入指定默认值  
         * @param string  $splitString 要分割的字符串 
         * @return unknown 
         */  
        function expSplit($SpiltObj,$isquest=false,$splitKey,$isaddActionString="-1",$splitString=',')  
        {  
            /*是数据集*/  
            $SpString="";  
            if($isquest)  
            {  
                $SpString=$isaddActionString;  
                while ($rt=mysql_fetch_array($SpiltObj)) {  
                    if(empty($SpString)||$SpString=="")  
                    $SpString=$rt[$splitKey];  
                    else  
                    $SpString.=$splitString.$rt[$splitKey];  
                }  
                unset($SpiltObj);  
                return $SpString;  
            }  
            else//是数据组  
            {  
                $SpString=$isaddActionString;  
                foreach ($SpiltObj as $key=>$val)  
                {  
                    if(empty($SpString)||$SpString=="")  
                    $SpString=$val[$splitKey];  
                    else  
                    $SpString.=$splitString.$val[$splitKey];  
                }  
                unset($SpiltObj);  
                return $SpString;  
            }  
            return $SpString;  
        }  
      
        /** 
         * 字符串加解密 
         * 
         * $str=$Function_Class->strCode("abc","哈哈","ENCODE"); 
         *$str=$Function_Class->strCode($str,"哈哈","DECODE"); 
         *  
         *  
         * @param string $string  要处理的字符串 
         * @param string $offKey  加密的偏移量    为空则为拿当前用户请求的IP 
         * @param string $action  ENCODE加密    DECODE解密 
         * @return string 
         */  
        function StrCode($string,$offKey="",$action='ENCODE'){  
            if($offKey=="")  
            $key    = substr(md5($_SERVER["HTTP_USER_AGENT"]),8,18);  
            else  
            $key=substr(md5($offKey),8,18);  
            unset($offKey);  
            $string = $action == 'ENCODE' ? $string : base64_decode($string);  
            $len    = strlen($key);  
            $code   = '';  
            for($i=0; $i<strlen($string); $i++){  
                $k      = $i % $len;  
                $code  .= $string[$i] ^ $key[$k];  
            }  
            $code = $action == 'DECODE' ? $code : base64_encode($code);  
            return $code;  
        }  
        /** 
         * 删除指定文件 
         * 
         * @param string $filename 
         */  
        function P_unlink($filename){  
            strpos($filename,'..')!==false && exit('Forbidden');  
            @unlink($filename);  
        }  
      
        /** 
         * 跳转到指定文件地址 
         * 
         * @param string $URL 
         */  
        function refreshto($URL){  
            header("Location: $URL");exit;  
        }  
      
        /** 
         * 返回日期时间格式 
         * 
         * @param int $timestamp  时间隔 
         * @param string $timeformat   返回格式 
         * @return string 
         */  
        function get_date($timestamp,$timeformat=''){  
            global $db_datefm,$_datefm,$_timedf;  
            $date_show=$timeformat ? $timeformat : ($_datefm ? $_datefm : $db_datefm);  
            return date($date_show,$timestamp+$_timedf*60);  
        }  
      
        /** 
         * 返回随机字符串  
         * 
         * @param int $length 要返回的长度 
         * @param int $mode  随机字符串的模式,提供了6种模式产生随机字符串 
         * @return unknown 
         */  
        function getRandomCode ($length = 32, $mode = 0)  
        {  
            switch ($mode) {  
                case '1':  
                    $str = '123456789';  
                    break;  
                case '2':  
                    $str = 'abcdefghijklmnopqrstuvwxyz';  
                    break;  
                case '3':  
                    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';  
                    break;  
                case '4':  
                    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';  
                    break;  
                case '5':  
                    $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';  
                    break;  
                case '6':  
                    $str = 'abcdefghijklmnopqrstuvwxyz1234567890';  
                    break;  
                default:  
                    $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';  
                    break;  
            }  
      
            $result = '';  
            $l = strlen($str)-1;  
            for($i = 0;$i < $length;$i ++){  
                $num = rand(0, $l);  
                $result .= $str[$num];  
            }  
            return $result;  
        }  
      
        /** 
         * 返回整数/或者浮点数的随机数 
         * 
         * @param int $lenth  返回的长度 
         * @return  string 
         */  
        function num_rand($lenth){  
            mt_srand((double)microtime() * 1000000);  
            for($i=0;$i<$lenth;$i++){  
                $randval.= mt_rand(0,9);  
            }  
            return $randval;  
        }  
      
        /** 
         *返回UTF8转GB2312字符串 
         * 
         * @param string $name  UTF8字符 
         * @return string 
         */  
        function utf8ToGb2312($name)  
        {  
            $tostr = "";  
            for($i=0;$i<strlen($name);$i++)  
            {  
                $curbin = ord(substr($name,$i,1));  
                if($curbin < 0x80)  
                {  
                    $tostr .= substr($name,$i,1);  
                }elseif($curbin < bindec("11000000")){  
                    $str = substr($name,$i,1);  
                    $tostr .= "&#".ord($str).";";  
                }elseif($curbin < bindec("11100000")){  
                    $str = substr($name,$i,2);  
                    $tostr .= "&#".GetUnicodeChar($str).";";  
                    $i += 1;  
                }elseif($curbin < bindec("11110000")){  
                    $str = substr($name,$i,3);  
                    $gstr= iconv("UTF-8","GB2312",$str);  
                    if(!$gstr)  
                    {  
                        $tostr .= "&#".GetUnicodeChar($str).";";  
                    }else{  
                        $tostr .= $gstr;  
                    }  
      
                    $i += 2;  
                }elseif($curbin < bindec("11111000")){  
                    $str = substr($name,$i,4);  
                    $tostr .= "&#".GetUnicodeChar($str).";";  
      
                    $i += 3;  
                }elseif($curbin < bindec("11111100")){  
                    $str = substr($name,$i,5);  
                    $tostr .= "&#".GetUnicodeChar($str).";";  
      
                    $i += 4;  
                }else{  
                    $str = substr($name,$i,6);  
                    $tostr .= "&#".GetUnicodeChar($str).";";  
      
                    $i += 5;  
                }  
            }  
            return $tostr;  
        }  
      
        /** 
      * 返回GB2312转UTF8字符集 
      * 
      * @param string $c  GB2312字符 
      * @return string 
      */  
        function gb2312ToUtf8($c) {  
            return iconv("GB2312","UTF-8",$c);  
        }  
      
        /** 
         * 格式化页数为数字形变量 
         * 
         * @param string/int $page  传引用变量参数 
         */   
        function Page_cv(&$page){  
        (!is_numeric($page) || $page < 1) && $page = 1;  
        }  
      
        /** 
         * 检查ID串入面是否不包括数字的ID,如果有刚返回FALSE 
         * 
         * @param string $selid  ID串 
         * @param string $splitString  分格字符串 
         * @return bool or string 
         */  
        function checkselid($selid,$splitString=","){  
            if(!$selid)return false;  
            if(is_array($selid)){  
                $extra=$ret='';  
                foreach($selid as $key => $value){  
                    if(!is_numeric($value)) return false;  
                    $ret.=$extra.$value;  $extra=$splitString;  
                }  
                return $ret;  
            }  
            else{  
                $dataarray=explode($splitString,$selid);  
                $isokdata=$this->checkselid($dataarray,$splitString);  
                if($isokdata==false) return false;  
                else    return $isokdata;  
            }  
        }  
      
        /** 
         * 检查传入来的URL是否正确 
         * 
         * @param stirng $checkUrl  
         * @return bool 
         */  
        function validateUrl($checkUrl)  
        {  
            if(!ereg("^(http|https|ftp)://((([a-zA-Z0-9-]+.){1,}[a-zA-Z]{2,4})|(localhost))(:[0-9]+){0,1}(/[a-zA-Z0-9-_,./+&%$#=~]+)*{1}quot;,$checkUrl))  
            return true;  
            else return false;  
        }  
      
      
        /** 
         * 返回保存目录信息 
         * 
         * @param defined $rootR_P  根目录 
         * @param bool $Transcoding 是否要生成转码目录 
         * @param string $server_type  应用类型,是加在目录前做来标识用 
         * @param string $attachpath  保存的第一级目录 
         * @param string $DirType  保存目录格式类型 
         * @return array   
         */  
        function CreateAttachmentDir($rootR_P,$Transcoding=false,$server_type="",$attachpath="attachment",$DirType="M")  
        {  
            if($attachpath=="") return false;  
            $DirType=strtoupper($DirType);  
            $Folder=array();  
            $savedir="";  
            switch ($DirType)  
            {  
                case 'Y':$savedir = $server_type.'_Year_'.date('Y');break;  
                case 'M':$savedir = $server_type.'_Mon_'.date('ym');break;  
                case 'D':$savedir = $server_type.'_Day_'.date('ymd');break;  
                default:$savedir = $server_type.'_Mon_'.date('ym');break;  
            }  
            if(!is_dir($rootR_P."$attachpath")) {  
                @mkdir($rootR_P."$attachpath");  
                @chmod($rootR_P."$attachpath", 0777);  
                @fclose(@fopen($rootR_P."$attachpath".'/index.html', 'w'));  
                @chmod($rootR_P."$attachpath".'/index.html', 0777);  
            }  
            if(!is_dir($rootR_P."$attachpath/$savedir")) {  
                @mkdir($rootR_P."$attachpath/$savedir");  
                @chmod($rootR_P."$attachpath/$savedir", 0777);  
                @fclose(@fopen($rootR_P."$attachpath/$savedir".'/index.html', 'w'));  
                @chmod($rootR_P."$attachpath/$savedir".'/index.html', 0777);  
            }  
            $source=$rootR_P."$attachpath/$savedir";  
            if(!is_dir($rootR_P."$attachpath/$savedir/image")) {  
                @mkdir($rootR_P."$attachpath/$savedir/image");  
                @chmod($rootR_P."$attachpath/$savedir/image", 0777);  
                @fclose(@fopen($rootR_P."$attachpath/$savedir/image".'/index.html', 'w'));  
                @chmod($rootR_P."$attachpath/$savedir/image".'/index.html', 0777);  
            }  
            if(!$Transcoding)  
            {  
                $source.="/image";  
                $savedir.="/image";  
            }  
            $Folder["parent_folder"]=$attachpath;  
            $Folder["child_folder"]=$savedir;  
            $Folder["save_folder"]=$source;  
            return $Folder;  
        }  
          
        /** 
         * 生成分割标签的HTML 
         * 
         * @param string $tags 标签 
         * @param string $requestPage请法度的地址 
         * @param string $className 连接的样式 
         * @return string 
         */  
        function splitTabs($tags,$requestPage,$className)  
        {  
            if(empty($tags)) return "";  
            $data=explode(",",$tags);  
            $html=" ";  
            $href="";  
            foreach ($data as $key=>$value)  
            {  
                $tempvalue=rawurlencode($value);  
                if(empty($requestPage))$href="javascript:void(0);";  
                else   
                    $href=$requestPage.$tempvalue;  
                $html.="  <a href='".$href."' class='$className'>$value</a>";  
                unset($tempvalue);  
            }  
            unset($href,$requestPage);  
            return $html;  
        }  
    }  
    ?>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值