<?php
//查询用户昵称
function uname($uid){
	$u = M("user")->select($uid);
	if($u[0]['uname'] == ''){
		return "匿名网友";
	}else{
		return $u[0]['uname'];
	}
}
//转换时间格式
function stime($date){
	return date("Y-m-d H:i:s",$date);
}
//查询话题分类名称
function memu($mid){
	$m = M("memu")->select($mid);
	return $m[0]['title'];
}
//查询会员积分
function money($uid){
	$m = M("user_date")->where("uid = ".$uid)->select();
	return $m[0]['money'];
}
//查询会员性别
function sex($uid){
	$s = M("user_date")->where("uid = ".$uid)->select();
	if($s[0]['sex'] == 1){
		return "女";
	}else{
		return "男";
	}
}
//获取勋章图片地址
function xunzhang($xid){
	$x = M("xunzhang_list")->select($xid);
	return $x[0]['url'];
}
//获取勋章简介
function xzabout($xid){
	$x = M("xunzhang_list")->select($xid);
	return $x[0]['about'];
}
//获取勋章url
function xz($xzid){
	$x = M("xunzhang_list")->select($xzid);
	return $x[0]['url'];
}
//获取会员头像
function uimg($uid){
	$file = "Uploads/avatar/{$uid}_small.jpg";
	if(!file_exists($file)){
		return 0;
	}else{
		return $uid;
	}
}
//查询月份
function bm($date){
	return date("m",$date);
}
//查询日
function bd($date){
	return date("d",$date);
}
//查询年
function by($date){
	return date("Y",$date);
}
//查询省
function sheng($from){
	$s = explode('-',$from);
	return $s[0];
}
//查询市
function city($from){
	$s = explode('-',$from);
	return $s[1];
}
//查询地区
function area($uid){
	$s = M("user_date")->where("uid = ".$uid)->select();
	$f = str_replace("省","",$s[0]['from']);
	$f = str_replace("市","",$f);
	return $f;
}
//替换省、市
function cfrom($s){
	$f = str_replace("省","",$s);
	$f = str_replace("市","",$f);
	return $f;
}
//查询年龄
function age($d){
	return date("Y") - date("Y",$d);
}
//查询年龄
function age2($uid){
	$d = M("user_date")->where("uid = ".$uid)->select();
	return date("Y") - date("Y",$d[0]['birth']);
}
//获取关注数
function gz($uid){
	$gz = M("follower")->where("fid = ".$uid)->count();
	return $gz;
}
//获取粉丝数
function fs($uid){
	$fs = M("follower")->where("uid = ".$uid)->count();
	return $fs;
}
//关注状态
function isfoll($uid){
	if($uid == $_SESSION['NR_USER_ID']){
		return "";
	}else{
		$u = M("follower")->where("uid = ".$uid." and fid = ".$_SESSION['NR_USER_ID'])->count();
		if($u){
			return "<div class='isgz'>已关注&nbsp;&nbsp;<a href='/unfoll_{$uid}.html' target='_self'>取消</a><div>";
		}else{
			return "<a href='/joinfoll_{$uid}.html' target='_self'><img src='/Public/Images/fbtn.jpg' /></a>";	
		}
	}
}
//关注状态
function isgz($uid){
	if($uid == $_SESSION['NR_USER_ID']){
		return "";
	}else{
		$u = M("follower")->where("uid = ".$uid." and fid = ".$_SESSION['NR_USER_ID'])->count();
		if($u){
			return "<font class='isgzz'>已关注&nbsp;&nbsp;<a href='/unfoll_{$uid}.html' target='_self'>取消</a><font>";
		}else{
			return "<a href='/joinfoll_{$uid}.html' target='_self'><img src='/Public/Images/fbtn.jpg' /></a>";	
		}
	}
}
//编辑权限
function isadmin($fid,$uid){
	if($_SESSION['NR_USER_ID'] == 1){
		return "<a href='/edforum_{$fid}.html'>编辑</a>&nbsp;&nbsp;<a href=javascript:window.confirm('是否要删除该话题?')?window.location.href='/delforum_{$fid}_{$uid}.html':void('')>删除</a>";
	}
	$f = M("forum")->select($fid);
	if($f[0]['pid'] == $_SESSION['NR_USER_ID']){
		return "<a href='/edforum_{$fid}.html'>编辑</a>";
	}
}
//转换html格式
function con($con){
	return html_entity_decode($con);
}
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset);
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix) return $slice."…";
    return $slice;
}
?>