关于php常用自定义函数_模板调用_输出自定义错误_取得文件扩展_分页函数以下文字资料是由3搜网小编为大家搜集整理后发布的内容,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“php常用自定义函数_模板调用_输出自定义错误_取得文件扩展_分页函数”文章吧。
/**
* 模板调用
*
* @param $module
* @param $template
* @param $istag
* @return unknown_type
*/
function template($module = 'content', $template = 'index', $style = '') {
if(strpos($module, 'plugin/')!== false) {
$plugin = str_replace('plugin/', '', $module);
return p_template($plugin, $template,$style);
}
$module = str_replace('/', DIRECTORY_SEPARATOR, $module);
if(!empty($style) && preg_match('/([a-z0-9\-_]+)/is',$style)) {
} elseif (empty($style) && !defined('STYLE')) {
if(defined('SITEID')) {
$siteid = SITEID;
} else {
$siteid = param::get_cookie('siteid');
}
if (!$siteid) $siteid = 1;
$sitelist = getcache('sitelist','commons');
if(!empty($siteid)) {
$style = $sitelist[$siteid]['default_style'];
}
} elseif (empty($style) && defined('STYLE')) {
$style = STYLE;
} else {
$style = 'default';
}
if(!$style) $style = 'default';
$template_cache = app_base::load_sys_class('template_cache');
$compiledtplfile = ROOT_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
if(file_exists(CODE_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
if(!file_exists($compiledtplfile) || (@filemtime(CODE_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {
$template_cache->template_compile($module, $template, $style);
}
} else {
$compiledtplfile = ROOT_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
if(!file_exists($compiledtplfile) || (file_exists(CODE_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(CODE_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {
$template_cache->template_compile($module, $template, 'default');
} elseif (!file_exists(CODE_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');
}
}
return $compiledtplfile;
}
/**
* 输出自定义错误
*
* @param $errno 错误号
* @param $errstr 错误描述
* @param $errfile 报错文件地址
* @param $errline 错误行号
* @return string 错误提示
*/
function my_error_handler($errno, $errstr, $errfile, $errline) {
if($errno==8) return '';
$errfile = str_replace(ROOT_PATH,'',$errfile);
if(app_base::load_config('system','errorlog')) {
error_log(''.date('m-d H:i:s',SYS_TIME).' | '.$errno.' | '.str_pad($errstr,30).' | '.$errfile.' | '.$errline."\r\n", 3, CACHE_PATH.'error_log.php');
} else {
$str = '
errorno:' . $errno . ',str:' . $errstr . ',file:' . $errfile . ',line' . $errline .'
Need Help?
';
echo $str;
}
}
/**
* 提示信息页面跳转,跳转地址如果传入数组,页面会提示多个地址供用户选择,默认跳转地址为数组的第一个值,时间为5秒。
* showmessage('登录成功', array('默认跳转地址'=>'http://www.baidu.com'));
* @param string $msg 提示信息
* @param mixed(string/array) $url_forward 跳转地址
* @param int $ms 跳转等待时间
*/
function showmessage($msg, $url_forward = 'goback', $ms = 1250, $dialog = '', $returnjs = '') {
if(defined('IN_ADMIN')) {
include(admin::admin_tpl('showmessage', 'admin'));
} else {
include(template('content', 'message'));
}
exit;
}
/**
* 查询字符是否存在于某字符串
*
* @param $haystack 字符串
* @param $needle 要查找的字符
* @return bool
*/
function str_exists($haystack, $needle)
{
return !(strpos($haystack, $needle) === FALSE);
}
/**
* 取得文件扩展
*
* @param $filename 文件名
* @return 扩展名
*/
function fileext($filename) {
return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
}
/**
* 加载模板标签缓存
* @param string $name 缓存名
* @param integer $times 缓存时间
*/
function tpl_cache($name,$times = 0) {
$filepath = 'tpl_data';
$info = getcacheinfo($name, $filepath);
if (SYS_TIME - $info['filemtime'] >= $times) {
return false;
} else {
return getcache($name,$filepath);
}
}
/**
* 写入缓存,默认为文件缓存,不加载缓存配置。
* @param $name 缓存名称
* @param $data 缓存数据
* @param $filepath 数据路径(模块名称) caches/cache_$filepath/
* @param $type 缓存类型[file,memcache,apc]
* @param $config 配置名称
* @param $timeout 过期时间
*/
function setcache($name, $data, $filepath='', $type='file', $config='', $timeout=0) {
app_base::load_sys_class('cache_factory','',0);
if($config) {
$cacheconfig = app_base::load_config('cache');
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->set($name, $data, $timeout, '', $filepath);
}
/**
* 读取缓存,默认为文件缓存,不加载缓存配置。
* @param string $name 缓存名称
* @param $filepath 数据路径(模块名称) caches/cache_$filepath/
* @param string $config 配置名称
*/
function getcache($name, $filepath='', $type='file', $config='') {
app_base::load_sys_class('cache_factory','',0);
if($config) {
$cacheconfig = app_base::load_config('cache');
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->get($name, '', '', $filepath);
}
//根据行政区划数字获取对应名称名称 如 110101 得到 北京市东城区
function get_div($div='')
{
if(empty($div)){
return false;
}
$regioncode = getcache('1','linkage');
if($regioncode)
{
$l1=substr($div,0,2).'0000';
$L1_n=$regioncode['data'][$l1]['name'];
$tb=array('110000','120000','310000','500000');
if(in_array($l1,$tb)){
$l2 = $l1=substr($div,0,2)."0000-r";
}else{
$l2 = substr($div,0,4)."00";
}
$L2_n = $regioncode['data'][$l2]['name'];
$L3_n = $regioncode['data'][$div]['name'];
if($L2_n===$L1_n){
$res_div = $L1_n.$L3_n;
}else{
$res_div = $L1_n.$L2_n.$L3_n;
}
return $res_div;
}else{
return '无行政区划地址';
}
}
//根据行业类型数字获取对应名称名称 如 $trade=‘1A0112’ 得到 农、林、牧、渔业-农业-谷物及其他作物的种植-薯类的种植
function get_trade_category($trade=''){
if(empty($trade)){
return false;
}
$trade_category = getcache('3','linkage');
if($trade_category){
$t1=substr($trade,0,2);
$T_1=$trade_category['data'][$t1]['name']?$trade_category['data'][$t1]['name']:'';
$t2=substr($trade,0,4);
$T_2=$trade_category['data'][$t2]['name']?$trade_category['data'][$t2]['name']:'';
$t3=substr($trade,0,5);
$T_3=$trade_category['data'][$t3]['name']?$trade_category['data'][$t3]['name']:'';
$T_4=$trade_category['data'][$trade]['name']?$trade_category['data'][$trade]['name']:'';
if($T_3===$T_4){
$res_trade = $T_1.'-'.$T_2.'-'.$T_3;
}else{
$res_trade = $T_1.'-'.$T_2.'-'.$T_3.'-'.$T_4;
}
if(empty($res_trade)){
return '行业类型数据不存在';
}
return $res_trade;
}else{
return '行业类型数据不存在';
}
}
/**
* 删除缓存,默认为文件缓存,不加载缓存配置。
* @param $name 缓存名称
* @param $filepath 数据路径(模块名称) caches/cache_$filepath/
* @param $type 缓存类型[file,memcache,apc]
* @param $config 配置名称
*/
function delcache($name, $filepath='', $type='file', $config='') {
app_base::load_sys_class('cache_factory','',0);
if($config) {
$cacheconfig = app_base::load_config('cache');
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->delete($name, '', '', $filepath);
}
/**
* 读取缓存,默认为文件缓存,不加载缓存配置。
* @param string $name 缓存名称
* @param $filepath 数据路径(模块名称) caches/cache_$filepath/
* @param string $config 配置名称
*/
function getcacheinfo($name, $filepath='', $type='file', $config='') {
app_base::load_sys_class('cache_factory');
if($config) {
$cacheconfig = app_base::load_config('cache');
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->cacheinfo($name, '', '', $filepath);
}
/**
* 生成sql语句,如果传入$in_cloumn 生成格式为 IN('a', 'b', 'c')
* @param $data 条件数组或者字符串
* @param $front 连接符
* @param $in_column 字段名称
* @return string
*/
function to_sqls($data, $front = ' AND ', $in_column = false) {
if($in_column && is_array($data)) {
$ids = '\''.implode('\',\'', $data).'\'';
$sql = "$in_column IN ($ids)";
return $sql;
} else {
if ($front == '') {
$front = ' AND ';
}
if(is_array($data) && count($data) > 0) {
$sql = '';
foreach ($data as $key => $val) {
$sql .= $sql ? " $front $key = '$val' " : " $key = '$val' ";
}
return $sql;
} else {
return $data;
}
}
}
/**
* 分页函数
*
* @param $num 信息总数
* @param $curr_page 当前分页
* @param $perpage 每页显示数
* @param $urlrule URL规则
* @param $array 需要传递的数组,用于增加额外的方法
* @return 分页
*/
function pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
if(defined('URLRULE') && $urlrule == '') {
$urlrule = URLRULE;
$array = $GLOBALS['URL_ARRAY'];
} elseif($urlrule == '') {
$urlrule = url_par('page={$page}');
}
$multipage = '';
if($num > $perpage) {
$page = $setpages+1;
$offset = ceil($setpages/2-1);
$pages = ceil($num / $perpage);
if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
$from = $curr_page - $offset;
$to = $curr_page + $offset;
$more = 0;
if($page >= $pages) {
$from = 2;
$to = $pages-1;
} else {
if($from <= 1) { $to = $page-1; $from = 2; } elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
//$multipage .= ''.$num.L('page_item').'';
if($curr_page>0) {
$multipage .= ' '.L('previous').'';
if($curr_page==1) {
$multipage .= ' 1';
} elseif($curr_page>6 && $more) {
$multipage .= ' 1..';
} else {
$multipage .= ' 1';
}
}
for($i = $from; $i <= $to; $i++) { if($i != $curr_page) { $multipage .= ' '.$i.''; } else { $multipage .= ' '.$i.'';
}
}
if($curr_page<$pages) { if($curr_page<$pages-5 && $more) { $multipage .= ' ..'.$pages.' '.L('next').''; } else { $multipage .= ' '.$pages.' '.L('next').''; } } elseif($curr_page==$pages) { $multipage .= ' '.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
return $multipage;
}
function pages1($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
if(defined('URLRULE') && $urlrule == '') {
$urlrule = URLRULE;
$array = $GLOBALS['URL_ARRAY'];
} elseif($urlrule == '') {
$urlrule = url_par('page={$page}');
}
$multipage = '';
if($num > $perpage) {
$page = $setpages+1;
$offset = ceil($setpages/2-1);
$pages = ceil($num / $perpage);
if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
$from = $curr_page - $offset;
$to = $curr_page + $offset;
$more = 0;
if($page >= $pages) {
$from = 2;
$to = $pages-1;
} else {
if($from <= 1) { $to = $page-1; $from = 2; } elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
//$multipage .= ''.$num.L('page_item').'';
if($curr_page>0) {
$multipage .= ' '.L('previous').'';
if($curr_page==1) {
$multipage .= ' 1';
} elseif($curr_page>6 && $more) {
$multipage .= ' 1..';
} else {
$multipage .= ' 1';
}
}
for($i = $from; $i <= $to; $i++) { if($i != $curr_page) { $multipage .= ' '.$i.''; } else { $multipage .= ' '.$i.'';
}
}
if($curr_page<$pages) { if($curr_page<$pages-5 && $more) { $multipage .= ' ..'.$pages.' '.L('next').''; } else { $multipage .= ' '.$pages.' '.L('next').''; } } elseif($curr_page==$pages) { $multipage .= ' '.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
return $multipage;
}
function pages2($num, $curr_page, $pages, $urlrule = '', $array = array(),$setpages = 10) {
if(defined('URLRULE') && $urlrule == '') {
$urlrule = URLRULE;
$array = $GLOBALS['URL_ARRAY'];
} elseif($urlrule == '') {
$urlrule = url_par('page={$page}');
}
$multipage = '';
if($pages > 1) {
$page = $setpages+1;
$offset = ceil($setpages/2-1);
if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
$from = $curr_page - $offset;
$to = $curr_page + $offset;
$more = 0;
if($page >= $pages) {
$from = 2;
$to = $pages-1;
} else {
if($from <= 1) { $to = $page-1; $from = 2; } elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
//$multipage .= ''.$num.L('page_item').'';
if($curr_page>0) {
$multipage .= ' '.L('previous').'';
if($curr_page==1) {
$multipage .= ' 1';
} elseif($curr_page>6 && $more) {
$multipage .= ' 1..';
} else {
$multipage .= ' 1';
}
}
for($i = $from; $i <= $to; $i++) { if($i != $curr_page) { $multipage .= ' '.$i.''; } else { $multipage .= ' '.$i.'';
}
}
if($curr_page<$pages) { if($curr_page<$pages-5 && $more) { $multipage .= ' ..'.$pages.' '.L('next').''; } else { $multipage .= ' '.$pages.' '.L('next').''; } } elseif($curr_page==$pages) { $multipage .= ' '.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
return $multipage;
}
/**
* 返回分页路径
*
* @param $urlrule 分页规则
* @param $page 当前页
* @param $array 需要传递的数组,用于增加额外的方法
* @return 完整的URL路径
*/
function pageurl($urlrule, $page, $array = array()) {
if(strpos($urlrule, '~')) {
$urlrules = explode('~', $urlrule);
$urlrule = $page < 2 ? $urlrules[0] : $urlrules[1]; } $findme = array('{$page}'); $replaceme = array($page); if (is_array($array)) foreach ($array as $k=>$v) {
$findme[] = '{$'.$k.'}';
$replaceme[] = $v;
}
$url = str_replace($findme, $replaceme, $urlrule);
$url = str_replace(array('http://','//','~'), array('~','/','http://'), $url);
return $url;
}
/**
* URL路径解析,pages 函数的辅助函数
*
* @param $par 传入需要解析的变量 默认为,page={$page}
* @param $url URL地址
* @return URL
*/
function url_par($par, $url = '') {
if($url == '') $url = get_url();
$pos = strpos($url, '?');
if($pos === false) {
$url .= '?'.$par;
} else {
$querystring = substr(strstr($url, '?'), 1);
parse_str($querystring, $pars);
$query_array = array();
foreach($pars as $k=>$v) {
if($k != 'page') $query_array[$k] = $v;
}
$querystring = http_build_query($query_array).'&'.$par;
$url = substr($url, 0, $pos).'?'.$querystring;
}
return $url;
} 以上就是关于“php常用自定义函数_模板调用_输出自定义错误_取得文件扩展_分页函数”这篇文章的内容,希望分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注3搜网技术频道。


