<?
//检查和注册外部提交的变量
function _RunMagicQuotes(&$svar)
{
	if(!get_magic_quotes_gpc())
	{
		if( is_array($svar) )
		{
			foreach($svar as $_k => $_v) $svar[$_k] = _RunMagicQuotes($_v);
		}
		else
		{
			$svar = addslashes(trim($svar));
		}
	}
	return $svar;
}
foreach(Array('_GET','_POST','_COOKIE') as $_request)
{
	foreach($$_request as $_k => $_v) ${$_k} = _RunMagicQuotes($_v);
}
//转义
function adds($str)
{
	return addslashes(trim($str));
}
//反转义
function unadds($str) {
	return stripslashes($str);
}
//过滤字符
function mytrim($str) {
	return adds(unadds(trim($str)));
}
//提示信息
function show($showstr,$showurl=0,$sc=2) {
	$sc=$sc*1000;
	if (empty($showurl)) {
		$showurl="javascript:window.history.back();";
	}
	if($showurl=='-1'){
		$showurl="javascript:self.location=document.referrer;";
	}
	$msg = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=gbk' />
			<script>setTimeout(\"window.location.href='{$showurl}'\",{$sc});</script>
			</head><body style='text-align:center'>
			<div style='width:600px;margin:100px auto;padding:20px;border:1px solid #ccc;background:#eff;'>
				<h3>{$showstr}</h3>
				<h4><a href='{$showurl}'>点击跳转...</a></h4>
			</div>
			</body></html>";
	die($msg);
}
//中文截取
function cutstr($string, $length, $dot = '')
{
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	$string = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
	$strcut = '';
	//gbk中文截取
		$dotlen = strlen($dot);
		$maxi = $length - $dotlen - 1;
		for($i = 0; $i < $maxi; $i++)
		{
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}

	$strcut = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;'), $strcut);
	return $strcut.$dot;
}
//时间格式转换
function mydate($time,$type=0) {
	if($type==0) {
		$str=date("Y-m-d",$time);
	}else{
		$str=date("Y-m-d H:i:s",$time);
	}
	return $str;
}
//url转换
function curl($tmp) {
	$tmp=md5($tmp);
	$tmp=substr($tmp,0,10);
	return $tmp;
}
//url转换
function xurl($tmp) {
	$tmp=urlencode($tmp);
	$tmp=str_replace("%","",$tmp);
	return $tmp;
}
//获取url参数
function geturl()
{
	$tmp="0";
	if (!empty($_SERVER['QUERY_STRING'])) {
		$tmp=$_SERVER['QUERY_STRING'];
	}
	$tmp=explode(".",$tmp);//去除.html等后缀
	$tmp=$tmp[0];
	$tmp=str_replace('/','',$tmp);
	return $tmp;
}
//获取url参数
function gethost1()
{
	$arr=explode(".",$_SERVER['HTTP_HOST']);
	$www=$arr[0];
	return $www;
}
//验证表单数据
function checkform($str,$strname,$len1=0,$len2=0) {
	if (empty($str)) {
		show($strname."不能为空！");
	}
	if (strlen($str)<$len1 || strlen($str)>$len2) {
		show($strname."必须在".$len1."至".$len2."个字符之间！");
	}
}
//去除非法数据
function check($tmp) {
	$tmp=str_replace("?","",$tmp);
	$tmp=str_replace("'","",$tmp);
	$tmp=str_replace("\r","",$tmp);
	$tmp=str_replace("\n","",$tmp);
	$tmp=str_replace(" ","",$tmp);
	$tmp=trim($tmp);
	return $tmp;
}
//获取真实IP
function getip()
{
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip_real = getenv("HTTP_CLIENT_IP");
	else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip_real = getenv("HTTP_X_FORWARDED_FOR");
	else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip_real = getenv("REMOTE_ADDR");
	else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip_real = $_SERVER['REMOTE_ADDR'];
	else
		$ip_real = "unknown";
	return($ip_real);
}
//去除html主函数
function Html2Text($str)
{
	$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
	$alltext = "";
	$start = 1;
	for($i=0;$i<strlen($str);$i++)
	{
		if($start==0 && $str[$i]==">")
		{
			$start = 1;
		}
		else if($start==1)
		{
			if($str[$i]=="<")
			{
				$start = 0;
				$alltext .= " ";
			}
			else if(ord($str[$i])>31)
			{
				$alltext .= $str[$i];
			}
		}
	}
	$alltext = str_replace("　"," ",$alltext);
	$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
	$alltext = preg_replace("/[ ]+/s"," ",$alltext);
	return $alltext;
}
//文本转HTML
function Text2Html($txt)
{
	$txt = str_replace("  ","　",$txt);
	$txt = str_replace("<","&lt;",$txt);
	$txt = str_replace(">","&gt;",$txt);
	$txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt);
	return $txt;
}
//页面跳转
function go($showurl,$sc=0) {
	echo "<meta HTTP-EQUIV=REFRESH CONTENT='{$sc};URL={$showurl}'>";
	exit();
}
//自动生成页面
function autohtml($reurl,$auto=1) {
	global $autohtml;
	if( !empty($autohtml)){
		if ( $autohtml>=$auto ) {
			echo "<p>自动更新中……</p>";
			echo "<meta HTTP-EQUIV=REFRESH CONTENT='1;URL=".$reurl."'>";
			exit();
		}else{
			echo "<p>【更新完毕！】</p>";
			exit();	
		}
	}
}
//标题seo
function seotitle($title){
	$title=str_replace(' ',',',$title); //替换空格
	$title=str_replace('_',',',$title); //替换下划线
	$arr=explode(',',$title);
	$zt=array();
	foreach($arr as $v){
		$v=trim($v);
		if(!empty($v) && strlen($v)>2){
			$zt[$v]=$v;
		}
	}
	$zt=implode(',',$zt);
	return $zt;
}
?>