基于Snoopy的PHP近似完美获取网站编码的代码
|
先要到网上下载Snoopy.class.php if (isset($titlematch) && is_array($titlematch) && count($titlematch) > 0) { $info['title'] = strip_tags($titlematch[1]); } preg_match_all('/<[s]meta[s]name="?' . '([^>"])"?[s]' . 'content="?([^>"])"?[s][/]?[s]>/si',$match); $ft=0; foreach($match[1] as $mt) { if($mt=="keywords" || $mt=="description") { $ft=1; } } if($ft==0) { preg_match_all('/<[s]meta[s]content="?([^>"])"?[s]name="?' . '([^>"])"?[s][/]?[s]*>/si',$match); if (isset($match) && is_array($match) && count($match) == 3) { $originals = $match[0]; $names = $match[2]; $values = $match[1]; if (count($originals) == count($names) && count($names) == count($values)) { $metaTags = array(); for ($i=0,$limiti=count($names); $i < $limiti; $i++) { $metaTags[$names[$i]] = array ( 'html' => htmlentities($originals[$i]), 'value' => $values[$i] ); } } } } else { if (isset($match) && is_array($match) && count($match) == 3) { $originals = $match[0]; $names = $match[1]; $values = $match[2]; if (count($originals) == count($names) && count($names) == count($values)) { $metaTags = array(); for ($i=0, 'value' => $values[$i] ); } } } } $result = array ( 'metaTags' => $metaTags ); if(isset($result['metaTags']['keywords']['value'])) { $info['keywords']=$result['metaTags']['keywords']['value']; } else { $info['keywords']=""; } if(isset($result['metaTags']['description']['value'])) { $info['desc']=$result['metaTags']['description']['value']; } else { $info['desc']=""; } $domain=preg_replace('/http:///si','',$this->url); $ip=@gethostbyname($domain); $ip_arr=explode(".",$ip); if(count($ip_arr)==4) { $info['ip']=$ip; } return $info; } public function t($string,$o) { for($i=0;$i<strlen($string);$i++) { if(ord($string{$i})<128) continue; if((ord($string{$i})&224)==224) { //第一个字节判断通过 $char = $string{++$i}; if((ord($char)&128)==128) { //第二个字节判断通过 $char = $string{++$i}; if((ord($char)&128)==128) { $encoding = "UTF-8"; break; } } } if((ord($string{$i})&192)==192) { //第一个字节判断通过 $char = $string{++$i}; if((ord($char)&128)==128) { //第二个字节判断通过 $encoding = "GB2312"; break; } } } return strtolower($encoding); } function uni_decode ($str,$code = 'utf-8'){ $str = json_decode(preg_replace_callback('/&#(d{5});/',create_function('$dec','return 'u'.dechex($dec[1]);'),'"'.$str.'"')); if($code != 'utf-8'){ $str = iconv('utf-8',$code,$str); } return $str; } //获取网站编码 public function getCharset() { if(!$this->open($this->url)){return false;exit;} //首先从html获取编码 preg_match("/<meta.+?charset=[^w]?([-w]+)/i",$temp) ? strtolower($temp[1]):""; if($temp[1]!="") { if(in_array($temp[1],$this->charset_arr)) { if($temp[1]=="gb2312") { $tmp_charset=$this->t($this->request->results,$temp[1]); if($tmp_charset==$temp[1]) { return $temp[1]; } } else { return $temp[1]; } } } if(!empty($this->request->headers)) { //从header中获取编码 $hstr=strtolower(implode("|||",$this->request->headers)); preg_match("/charset=[^w]?([-w]+)/is",$hstr,$lang) ? strtolower($lang[1]):""; if($lang[1]!="") { return $lang[1]; } } $encode_arr=array("UTF-8","GB2312","GBK","BIG5","ASCII","EUC-JP","Shift_JIS","CP936","ISO-8859-1","JIS","eucjp-win","sjis-win"); $encoded=mb_detect_encoding($this->request->results,$encode_arr); if($encoded) { return strtolower($encoded); } else { return false; } } } ?> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
