newInstanceArgs($args);
}
/**
* 生产controller对象
*/
function init_controller($controller_name){
if (!class_exists($controller_name)) {
$model_file = CONTROLLER_DIR . strtolower($controller_name) . '.class.php';
if(!is_file($model_file)){
return false;
}
require_once ($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)) {
require($filename);
}else{
pr($filename.' is not exist',1);
}
}
/**
* 加载函数库
*/
function load_function($function){
$filename = FUNCTION_DIR.$function.'.function.php';
if (file_exists($filename)) {
require($filename);
}else{
pr($filename.' is not exist',1);
}
}
/**
* 文本字符串转换
*/
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);
}
}
/**
* 将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;
}
}
/**
* 计算时间差
*
* @param char $pretime
* @return char
*/
function spend_time(&$pretime){
$now = microtime(1);
$spend = round($now - $pretime, 5);
$pretime = $now;
return $spend;
}
/**
* 返回当前浮点式的时间,单位秒;主要用在调试程序程序时间时用
*
* @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);
}
}
/**
* 二维数组按照指定的键值进行排序,
*
* @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($arr,$index){
foreach($arr as $k=>$v){
$index--;
if($index<0) return array($k,$v);
}
}
/**
* 打包返回AJAX请求的数据
* @params {int} 返回状态码, 通常0表示正常
* @params {array} 返回的数据集合
*/
function show_json($data,$code = true,$info=''){
$use_time = mtime() - $GLOBALS['config']['app_startTime'];
$result = array('code' => $code,'use_time'=>$use_time,'data' => $data);
if ($info != '') {
$result['info'] = $info;
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
exit;
}
/**
* 简单模版转换,用于根据配置获取列表:
* 参数:cute1:第一次切割的字符串,cute2第二次切割的字符串,
* arraylist为待处理的字符串,$this 为标记当前项,$this_str为当项标记的替换。
* $tpl为处理后填充到静态模版({0}代表切割后左值,{1}代表切割后右值,{this}代表当前项填充值)
* 例子:
* $arr="default=淡蓝(默认)=5|mac=mac海洋=6|mac1=mac1海洋=7";
* $tpl="
'si", "'
'si", "''si", // 去掉 javascript "'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记 "'([\r\n])[\s]+'", // 去掉空白字符 ); // 作为 PHP 代码运行 $replace = array ("#img#\\1#/img#", "#link#\\1#\\2#/link#", "[br]", "", "[br]", "", "", "", ); $text = preg_replace ($search, $replace, $content); $text = str_replace("[br]", "' . get_var_name($var) . ' = ' . $out . ''; if ($exit) exit; //为真则退出 } /** * 调试输出变量,对象的值。 参数任意个(任意类型的变量) * * @return echo */ function debug_out(){ $avg_num = func_num_args(); $avg_list = func_get_args(); ob_start(); for($i = 0; $i < $avg_num; $i++) { pr($avg_list[$i]); } $out = ob_get_clean(); echo $out; exit; } /** * 取$from~$to范围内的随机数 * * @param $from 下限 * @param $to 上限 * @return unknown_type */ function rand_from_to($from, $to){ $size = $from - $to; //数值区间 $max = 30000; //最大 if ($size < $max) { return $from + mt_rand(0, $size); } else { if ($size % $max) { return $from + random_from_to(0, $size / $max) * $max + mt_rand(0, $size % $max); } else { return $from + random_from_to(0, $size / $max) * $max + mt_rand(0, $max); } } } /** * 产生随机字串,可用来自动生成密码 默认长度6位 字母和数字混合 * * @param string $len 长度 * @param string $type 字串类型:0 字母 1 数字 2 大写字母 3 小写字母 4 中文 * 其他为数字字母混合(去掉了 容易混淆的字符oOLl和数字01,) * @param string $addChars 额外字符 * @return string */ function rand_string($len = 4, $type = ''){ $str = ''; switch ($type) { case 0://大小写中英文 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; case 1://数字 $chars = str_repeat('0123456789', 3); break; case 2://大写字母 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 3://小写字母 $chars = 'abcdefghijklmnopqrstuvwxyz'; break; default: // 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数 $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'; break; } if ($len > 10) { // 位数过长重复字符串一定次数 $chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5); } if ($type != 4) { $chars = str_shuffle($chars); $str = substr($chars, 0, $len); } else { // 中文随机字 for($i = 0; $i < $len; $i ++) { $str .= msubstr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1); } } return $str; } /** * php DES解密函数 * * @param string $key 密钥 * @param string $encrypted 加密字符串 * @return string */ function des_decode($key, $encrypted){ $encrypted = base64_decode($encrypted); $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); //使用MCRYPT_DES算法,cbc模式 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); mcrypt_generic_init($td, $key, $key); //初始处理 $decrypted = mdecrypt_generic($td, $encrypted); //解密 mcrypt_generic_deinit($td); //结束 mcrypt_module_close($td); return pkcs5_unpad($decrypted); } /** * php DES加密函数 * * @param string $key 密钥 * @param string $text 字符串 * @return string */ function des_encode($key, $text){ $y = pkcs5_pad($text); $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); //使用MCRYPT_DES算法,cbc模式 $ks = mcrypt_enc_get_key_size($td); mcrypt_generic_init($td, $key, $key); //初始处理 $encrypted = mcrypt_generic($td, $y); //解密 mcrypt_generic_deinit($td); //结束 mcrypt_module_close($td); return base64_encode($encrypted); } function pkcs5_unpad($text){ $pad = ord($text{strlen($text)-1}); if ($pad > strlen($text)) return $text; if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text; return substr($text, 0, -1 * $pad); } function pkcs5_pad($text, $block = 8){ $pad = $block - (strlen($text) % $block); return $text . str_repeat(chr($pad), $pad); }