PHP 实用函数库
2016-05-18 20:45
330 查看
/** *一组打印输出,用作调试的函数 **/ 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 9a32 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; }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- php7 读取php.ini[4]
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法