PHP 实用函数库

/**
*一组打印输出,用作调试的函数
**/
function p($args=''){
    $args = func_get_args();
    $ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;

    if (! $ajax) echo '<pre style="background:#efefef;padding:5px 0 0 5px;border: 1px solid #aaa;">';

    foreach ($args as $key => $data) {
        if (is_bool($data)) {
            $data = $data ? '#true' : '#false';
        } elseif (empty($data)){
            if (is_null($data)) $data = '#null';
            if ($data === '') $data = '#empty';
        }

        if ($ajax) {
            echo "\r\n"; print_r($data); echo "\r\n";
        } else {
            print_r($data); echo '<br/>';
        }
    }
    if (! $ajax) echo '</pre><br/>';
}


function e($args = ''){
    if (in_array($args, ['@post',  '@get'])) {
        if ($args == '@post') $args = $_POST;
        if ($args == '@get') $args = $_GET;
    }
    p($args); exit;
}

function post(){
    p($_POST);
}

function get(){
    p($_GET);
}

function flog($data, $file='debug.txt', $live=''){
   if(!empty($live) && strtotime($live) - time() < 0) return false;
    if(empty($file)) $file = './logs_'.date('Y-m-d').'.log';
    if($data === '@read'){
        echo '<pre>'.file_get_contents($file) .'</pre>';
    }else if($data === '@unlink'){
        return unlink($file);
    }else{
        file_put_contents($file, var_export($data, true) ."\r\n================[time ".date('H:i:s')."]================\r\n", FILE_APPEND | LOCK_EX);
    }
}

/**
 * 删除清除bom头
 * $content 文件内容
 */
function clear_bom($content){
    if(substr($content, 0, 3) == chr(239).chr(187).chr(191)){
        $content = substr($content, 3);
    }
    return $content;
}


/**
 * 判断当前时间在一段时间之内
 * @param string $start
 * @param string $end
 * @param string $config
 * @return string
 */
function time_period($start, $end, $config='auto'){
    $nowtime = time();
    $start = strtotime($start);
    $end = strtotime($end);
    $key = ($nowtime < $start)? 'wait' : (($nowtime > $end)? 'end' : 'ing');
    if($config == 'auto') $config = array('wait'=>'未开始', 'ing'=>'进行中', 'end'=>'已结束');
    return is_array($config)? $config[$key] : $key;
}


/**
 * 对象转数组
 * @param object $object
 * @return void|array
 */
function object_to_array($object){
    $object = (array)$object;
    foreach($object as $k=>$v){
        if(gettype($v) == 'resource') return;
        if(gettype($v) == 'object' || gettype($v)=='array')$object[$k] = (array)object_to_array($v);
    }
    return $object;
}


/**
 * 文本文件转数组:
 * 文件内容:
 北京   111
 天津   222
 * @param strint $content 文件路径或文本内容
 * @return array
 */
function text2array($content){
    if(is_file($content)) $content = file_get_contents($content);
    $content = array_filter(explode("\r\n", trim($content)));
    foreach($content as $value){
        $data[] = array_values(array_filter(explode(' ', $value)));
    }
    return $data;
}


/**
 * 格式化时间,防止0或空被转成1970年
 * @param unknown $format 格式参数
 * @param string $timestamp 时间戳
 * @param string $default 时间为空时返回默认值
 */
function dateformat($timestamp='', $format='', $default='--'){
    $styles = array('auto'=>'Y-m-d H:i:s');
    if(empty($format)) $format = 'auto';
    if(isset($styles[$format])) $format = $styles[$format];
    if(!is_numeric($timestamp)) $timestamp = strtotime($timestamp);
    return empty($timestamp)? $default : date($format, $timestamp);

}



/**
 * 低版本的PHP实现
 * json_encode的JSON_UNESCAPED_UNICODE参数
 * @param array $array
 * @return string|mixed
 */
function jsonencode($array){
    if(version_compare(PHP_VERSION,'5.4.0','<')){
        $str = json_encode( $array);
        $str =  preg_replace_callback("#\\\u([0-9a-f]{4})#i", function($matchs){return iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1])); },$str);
        return  $str;
    }else{
        return json_encode($array, JSON_UNESCAPED_UNICODE);
    }
}

/**
 * 二维数组保存成 csv 文件
 * @param array $data 二维数组
 * @param string $file 文件路径
 * @param array $header 表头配置
 * @param bool $download 是否以下载方式,否则在服务器上写入文件
 $head = array('name'=>'名字', 'age'=>'年龄');
 $data = array(
 array('age'=>34, 'name'=>'张三'),
 array('age'=>15, 'name'=>'李四'),
 );
 save_csv($data, 'D:/test.csv', $head, false);
 */
function save_csv($data, $file, $head=array(), $download=true){

    $file = iconv('UTF-8', 'GBK', $file);
    if(empty($head)){
        $head = current($data);
    }else if($download || !file_exists($file)){
        array_unshift($data, $head);
    }
    if($download == true){
        header('Content-Type:application/application/octet-stream');
        header("Content-Disposition:attachment;filename=$file");
        $fp = tmpfile();
    }else{
        $fp = fopen($file, 'a');
    }
    foreach($data as $row){
        $line = array();
        foreach($head as $field => $name){
            $line[] =  trim($row[$field]);
        }
        fputcsv($fp, $line);
    }
    if($download){
        fseek($fp, 0); echo stream_get_contents($fp);
    }
    fclose($fp);

}


    /**
     * 中文字姓名截取
     * 规则:两个显示(头***),三个级以上显示(头***尾)
     * @param string $string 名字
     * @param string $encode 编码
     * @param string $repeat 填充符号
     * @param number $count 填充次数
     * @return string
     */
   function cut_name($string, $encode='UTF-8', $repeat='*', $count=3){
        $length = mb_strlen($string, $encode);
        if($length < 4){
            $head = mb_substr($string, 0, 1, $encode);
            $foot = ($length > 2)? mb_substr($string, -1, 1, $encode) : '';
        }else{
            $head = mb_substr($string, 0, 2, $encode);
            $foot = mb_substr($string, -1, 1, $encode);
        }
        return $head . str_repeat($repeat, $count). $foot;
    }


    /**
     * 取得月份的最后一天
     * @param string $nowtime
     * @param number $addmonth
     * @return number
     */
    function get_month_day($nowtime, $addmonth = 1){
        $strtime = strtotime('+'.$addmonth. ' month', $nowtime);
        if(date('j', $nowtime) != date('j', $strtime)){
            $strtime = strtotime('-1 day', strtotime(date('Y-m-1',$strtime)));
        }
        return $strtime;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值