newInstanceArgs($args);
}
/**
* 生产controller对象
*/
function init_controller($controller_name){
if (!class_exists($controller_name)) {
$model_file = CONTROLLER_DIR.$controller_name.'.class.php';
if(!is_file($model_file)){
return false;
}
include($model_file);
}
$reflectionObj = new ReflectionClass($controller_name);
$args = func_get_args();
array_shift($args);
return $reflectionObj -> newInstanceArgs($args);
}
/**
* 加载类
*/
function load_class($class){
$filename = CLASS_DIR.$class.'.class.php';
if (file_exists($filename)) {
include($filename);
}else{
show_tips($filename.' is not exist');
}
}
/**
* 加载函数库
*/
function load_function($function){
$filename = FUNCTION_DIR.$function.'.function.php';
if (file_exists($filename)) {
include($filename);
}else{
show_tips($filename.' is not exist');
}
}
/**
* 文本字符串转换
*/
function mystr($str){
$from = array("\r\n", " ");
$to = array("
", " ");
return str_replace($from, $to, $str);
}
// 清除多余空格和回车字符
function strip($str){
return preg_replace('!\s+!', '', $str);
}
/**
* 获取精确时间
*/
function mtime(){
$t= explode(' ',microtime());
$time = $t[0]+$t[1];
return $time;
}
/**
* 过滤HTML
*/
function clear_html($HTML, $br = true){
$HTML = htmlspecialchars(trim($HTML));
$HTML = str_replace("\t", ' ', $HTML);
if ($br) {
return nl2br($HTML);
} else {
return str_replace("\n", '', $HTML);
}
}
/**
* 过滤js、css等
*/
function filter_html($html){
$find = array(
"/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU",
"/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
"/javascript\s*:/isU",
);
$replace = array("<\\1\\2\\3>","\\1\\2","");
return preg_replace($find,$replace,$html);
}
/**
* 将obj深度转化成array
*
* @param $obj 要转换的数据 可能是数组 也可能是个对象 还可能是一般数据类型
* @return array || 一般数据类型
*/
function obj2array($obj){
if (is_array($obj)) {
foreach($obj as &$value) {
$value = obj2array($value);
}
return $obj;
} elseif (is_object($obj)) {
$obj = get_object_vars($obj);
return obj2array($obj);
} else {
return $obj;
}
}
function ignore_timeout(){
@ignore_user_abort(true);
@set_time_limit(24 * 60 * 60);//set_time_limit(0) 1day
@ini_set('memory_limit', '2028M');//2G;
}
/**
* 计算时间差
*
* @param char $pretime
* @return char
*/
function spend_time(&$pretime){
$now = microtime(1);
$spend = round($now - $pretime, 5);
$pretime = $now;
return $spend;
}
function check_code($code){
ob_clean();
header("Content-type: image/png");
$width = 70;$height=27;
$fontsize = 18;$len = strlen($code);
$im = @imagecreatetruecolor($width, $height) or die("create image error!");
$background_color = imagecolorallocate($im,255, 255, 255);
imagefill($im, 0, 0, $background_color);
for ($i = 0; $i < 2000; $i++) {//获取随机淡色
$line_color = imagecolorallocate($im, mt_rand(180,255),mt_rand(160, 255),mt_rand(100, 255));
imageline($im,mt_rand(0,$width),mt_rand(0,$height), //画直线
mt_rand(0,$width), mt_rand(0,$height),$line_color);
imagearc($im,mt_rand(0,$width),mt_rand(0,$height), //画弧线
mt_rand(0,$width), mt_rand(0,$height), $height, $width,$line_color);
}
$border_color = imagecolorallocate($im, 160, 160, 160);
imagerectangle($im, 0, 0, $width-1, $height-1, $border_color);//画矩形,边框颜色200,200,200
for ($i = 0; $i < $len; $i++) {//写入随机字串
$text_color = imagecolorallocate($im,mt_rand(30, 140),mt_rand(30,140),mt_rand(30,140));
imagechar($im,10,$i*$fontsize+6,rand(1,$height/3),$code[$i],$text_color);
}
imagejpeg($im);//显示图
imagedestroy($im);//销毁图片
}
/**
* 返回当前浮点式的时间,单位秒;主要用在调试程序程序时间时用
*
* @return float
*/
function microtime_float(){
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
/**
* 计算N次方根
* @param $num
* @param $root
*/
function croot($num, $root = 3){
$root = intval($root);
if (!$root) {
return $num;
}
return exp(log($num) / $root);
}
function add_magic_quotes($array){
foreach ((array) $array as $k => $v) {
if (is_array($v)) {
$array[$k] = add_magic_quotes($v);
} else {
$array[$k] = addslashes($v);
}
}
return $array;
}
// 字符串加转义
function add_slashes($string){
if (!$GLOBALS['magic_quotes_gpc']) {
if (is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = add_slashes($val);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/**
* hex to binary
*/
if (!function_exists('hex2bin')) {
function hex2bin($hexdata) {
return pack('H*', $hexdata);
}
}
if (!function_exists('gzdecode')) {
function gzdecode($data){
return gzinflate(substr($data,10,-8));
}
}
/**
* 二维数组按照指定的键值进行排序,
*
* @param $keys 根据键值
* @param $type 升序降序
* @return array $array = array(
* array('name'=>'手机','brand'=>'诺基亚','price'=>1050),
* array('name'=>'手表','brand'=>'卡西欧','price'=>960)
* );$out = array_sort($array,'price');
*/
function array_sort($arr, $keys, $type = 'asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k => $v) {
$keysvalue[$k] = $v[$keys];
}
if ($type == 'asc') {
asort($keysvalue);
} else {
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
/**
* 遍历数组,对每个元素调用 $callback,假如返回值不为假值,则直接返回该返回值;
* 假如每次 $callback 都返回假值,最终返回 false
*
* @param $array
* @param $callback
* @return mixed
*/
function array_try($array, $callback){
if (!$array || !$callback) {
return false;
}
$args = func_get_args();
array_shift($args);
array_shift($args);
if (!$args) {
$args = array();
}
foreach($array as $v) {
$params = $args;
array_unshift($params, $v);
$x = call_user_func_array($callback, $params);
if ($x) {
return $x;
}
}
return false;
}
// 求多个数组的并集
function array_union(){
$argsCount = func_num_args();
if ($argsCount < 2) {
return false;
} else if (2 === $argsCount) {
list($arr1, $arr2) = func_get_args();
while ((list($k, $v) = each($arr2))) {
if (!in_array($v, $arr1)) $arr1[] = $v;
}
return $arr1;
} else { // 三个以上的数组合并
$arg_list = func_get_args();
$all = call_user_func_array('array_union', $arg_list);
return array_union($arg_list[0], $all);
}
}
// 取出数组中第n项
function array_get_index($arr,$index){
foreach($arr as $k=>$v){
$index--;
if($index<0) return array($k,$v);
}
}
//set_error_handler('errorHandler',E_ERROR|E_PARSE|E_CORE_ERROR|E_COMPILE_ERROR|E_USER_ERROR);
register_shutdown_function('fatalErrorHandler');
function errorHandler($err_type,$errstr,$errfile,$errline){
if (($err_type & E_WARNING) === 0 && ($err_type & E_NOTICE) === 0) {
return false;
}
$arr = array(
$err_type,
$errstr,
//" in [".$errfile.']',
" in [".get_path_this(get_path_father($errfile)).'/'.get_path_this($errfile).']',
'line:'.$errline,
);
$str = implode(" ",$arr)."
";
show_tips($str);
}
//捕获fatalError
function fatalErrorHandler(){
$e = error_get_last();
switch($e['type']){
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
errorHandler($e['type'],$e['message'],$e['file'],$e['line']);
break;
case E_NOTICE:break;
default:break;
}
}
function show_tips($message,$url= '', $time = 3){
ob_get_clean();
header('Content-Type: text/html; charset=utf-8');
$goto = "content='$time;url=$url'";
$info = "Auto jump after {$time}s, Click Here";
if ($url == "") {
$goto = "";
$info = "";
} //是否自动跳转
$message = filter_html(nl2br($message));
echo<<
警告 (Warning!)
$message
$info