function auto_read($str, $charset='UTF-8') { $list = array('EUC-JP', 'Shift_JIS', 'UTF-8', 'iso-2022-jp'); $encode = mb_detect_encoding($str, $list); // echo $encode;die(); if($encode == 'UTF-8'){ return $str; }else{ return mb_convert_encoding($str, $charset, $encode); } } function detect_encoding($file){ $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1'); $str = file_get_contents($file); foreach ($list as $item) { $tmp = mb_convert_encoding($str, $item, $item); if (md5($tmp) == md5($str)) { return $item; } } return null; } function curl_get_from_webpage($url,$proxy='',$loop=10){ $data = false; $i = 0; while(!$data) { $data = curl_get_from_webpage_one_time($url,$proxy); if($i++ >= $loop) break; } return $data; } function curl_get_from_webpage_one_time($url,$proxy=''){ if(function_exists("curl_init") && function_exists("curl_setopt") && function_exists("curl_exec") && function_exists("curl_close")){ $curl = curl_init(); //如果有用代理,则使用代�? $user_agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"; $urlReferer = "http://www.google.com"; if(strlen($proxy) > 8) curl_setopt($curl, CURLOPT_PROXY, $proxy); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_REFERER, $urlReferer); curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); $data=curl_exec($curl); curl_close($curl); }else{ $is_auf=ini_get('allow_url_fopen') && function_exists("file_get_contents")?true:false; if($is_auf){ $data = file_get_contents($url); } } if(!$data) return false; return $data; } function is_ip($localIp,$ipRanges) { $localIp = ip2long($localIp); foreach($ipRanges as $val) { $ipmin=sprintf("%u",ip2long($val[0])); $ipmax=sprintf("%u",ip2long($val[1])); if($localIp >= $ipmin && $localIp <= $ipmax) { return true; } } return false; } function get_real_ip(){ $ip=false; if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } for ($i = 0; $i < count($ips); $i++) { if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) { $ip = $ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } //file end