<?php
include("config.php");

function axing_connect()
{
global $host,$root,$key,$base;
@mysql_connect($host,$root,$key) or die("数据库连接失败");
@mysql_select_db($base);
@mysql_query("SET NAMES 'gb2312'");
}


function get_url_content($url) {
if(function_exists('file_get_contents')) {
@$file_contents = file_get_contents($url) or die("暂时无法获取网站信息，请稍候再试！如果一直无法获取，说明是目标网站或者服务器限制！");
} else {
$ch = curl_init();
$timeout = 5; 
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
function get_encoding($data,$to)
{
$encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');
$encoded = mb_detect_encoding($data, $encode_arr);
$data = mb_convert_encoding($data,$to,$encoded);
return $data;
}


//去除所有js,html,css代码 
function get_enhtml($string){
$pattern=array ("'<script[^>]*?>.*?</script>'si",// 去掉 javascript
"'<style[^>]*?>.*?</style>'si",// 去掉 HTML 标记
"'<[/!]*?[^<>]*?>'si",//去掉 HTML 标记
"'<!--[/!]*?[^<>]*?>'si", // 去掉 注释标记
"'([rn])[s]+'", // 去掉空白字符
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$replace=array ("", "", "", "", "\1", "", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\1)");
$str=preg_replace($pattern, $replace, $string);
//删除换行符
$str = preg_replace( "/(\r?\n)/", "",$str);
//过滤空格
$str = str_replace( "	", "",$str);
$str = str_replace( "　", "",$str);
//过滤非法字符
$str = preg_replace( "/[ '.,;*?~`!#$%^+=)(<>{}]|\]|\[|\/|\\\|\"|\|/", "",$str);
return $str;
}



//截取函数
function FSubstr($title,$start,$len="",$magic=true) 
{ 


$length = 0; 
if($len == "") $len = strlen($title); 



//判断起始为不正确位置 
if($start > 0) 
{ 
$cnum = 0; 
for($i=0;$i<$start;$i++) 
{ 
if(ord(substr($title,$i,1)) >= 128) $cnum ++; 
} 
if($cnum%2 != 0) $start--; 

unset($cnum); 
} 



if(strlen($title)<=$len) return substr($title,$start,$len); 



$alen = 0; 
$blen = 0; 



$realnum = 0; 



for($i=$start;$i<strlen($title);$i++) 
{ 
$ctype = 0; 
$cstep = 0; 
$cur = substr($title,$i,1); 
if($cur == "&") 
{ 
if(substr($title,$i,4) == "<") 
{ 
$cstep = 4; 
$length += 4; 
$i += 3; 
$realnum ++; 
if($magic) 
{ 
$alen ++; 
} 
} 
else if(substr($title,$i,4) == ">") 
{ 
$cstep = 4; 
$length += 4; 
$i += 3; 
$realnum ++; 
if($magic) 
{ 
$alen ++; 
} 
} 
else if(substr($title,$i,5) == "&") 
{ 
$cstep = 5; 
$length += 5; 
$i += 4; 
$realnum ++; 
if($magic) 
{ 
$alen ++; 
} 
} 
else if(substr($title,$i,6) == "") 
{ 
$cstep = 6; 
$length += 6; 
$i += 5; 
$realnum ++; 
if($magic) 
{ 
$alen ++; 
} 
} 
else if(substr($title,$i,6) == "'") 
{ 
$cstep = 6; 
$length += 6; 
$i += 5; 
$realnum ++; 
if($magic) 
{ 
$alen ++; 
} 
} 
else if(preg_match("/&#(\d+);/i",substr($title,$i,8),$match)) 
{ 
$cstep = strlen($match[0]); 
$length += strlen($match[0]); 
$i += strlen($match[0])-1; 
$realnum ++; 
if($magic) 
{ 
$blen ++; 
$ctype = 1; 
} 
} 
}else{ 
if(ord($cur)>=128) 
{ 
$cstep = 2; 
$length += 2; 
$i += 1; 
$realnum ++; 
if($magic) 
{ 
$blen ++; 
$ctype = 1; 
} 
}else{ 
$cstep = 1; 
$length +=1; 
$realnum ++; 
if($magic) 
{ 
if(ord($cur) >= 65 && ord($cur) <= 90) 
{ 
$blen++; 
}else{ 
$alen++; 
} 
} 
} 
} 

if($magic) 
{ 
if(($blen*2+$alen) == ($len*2)) break; 
if(($blen*2+$alen) == ($len*2+1)) 
{ 
if($ctype == 1) 
{ 
$length -= $cstep; 
break; 
}else{ 
break; 
} 
} 
}else{ 
if($realnum == $len) break; 
} 
} 



unset($cur); 
unset($alen); 
unset($blen); 
unset($realnum); 
unset($ctype); 
unset($cstep); 



return substr($title,$start,$length); 
} 
error_reporting(0);

?>