基于Snoopy的PHP近似完美获取网站编码的代码


Posted in PHP onOctober 23, 2011

先要到网上下载Snoopy.class.php
调用方法:

<?php 
require 'lib/Snoopy.class.php'; 
require 'lib/WebCrawl.class.php';//包含下面代码 
$go=new WebCrawl('http://www.baidu.com'); 
echo $go->getCharset(); 
?>

<?php 
class WebCrawl 
{ 
private $url; 
private $request; 
public $charset_arr=array( 
'gb2312', 
'utf-8', 
'big5', 
'gbk', 
'ascii', 
'cp936', 
'ibm037', 
'ibm437', 
'ibm500', 
'asmo-708', 
'dos-720', 
'ibm737', 
'ibm775', 
'ibm850', 
'ibm852', 
'ibm855', 
'ibm857', 
'ibm00858', 
'ibm861', 
'ibm860', 
'dos-862', 
'ibm863', 
'ibm864', 
'ibm865', 
'cp866', 
'ibm869', 
'ibm870', 
'windows-874', 
'cp875', 
'shift_jis', 
'ks_c_5601-1987', 
'ibm1026', 
'ibm01047', 
'ibm01047', 
'ibm01040', 
'ibm01041', 
'ibm01042', 
'ibm01043', 
'ibm01044', 
'ibm01045', 
'ibm01046', 
'ibm01047', 
'ibm01048', 
'ibm01049', 
'utf-16', 
'unicodefffe', 
'windows-1250', 
'windows-1251', 
'windows-1252', 
'windows-1253', 
'windows-1254', 
'windows-1255', 
'windows-1256', 
'windows-1257', 
'windows-1258', 
'johab', 
'macintosh', 
'x-mac-japanese', 
'x-mac-chinesetrad', 
'x-mac-korean', 
'x-mac-arabic', 
'x-mac-hebrew', 
'x-mac-greek', 
'x-mac-cyrillic', 
'x-mac-chinesesimp', 
'x-mac-romanian', 
'x-mac-ukrainian', 
'x-mac-thai', 
'x-mac-ce', 
'x-mac-icelandic', 
'x-mac-turkish', 
'x-mac-croatian', 
'x-chinese-cns', 
'x-cp20001', 
'x-chinese-eten', 
'x-cp20003', 
'x-cp20004', 
'x-cp20005', 
'x-ia5', 
'x-ia5-german', 
'x-ia5-swedish', 
'x-ia5-norwegian', 
'us-ascii', 
'x-cp20261', 
'x-cp20269', 
'ibm273', 
'ibm277', 
'ibm278', 
'ibm280', 
'ibm284', 
'ibm285', 
'ibm290', 
'ibm420', 
'ibm423', 
'ibm424', 
'x-ebcdic-koreanextended', 
'ibm-thai', 
'koi8-r', 
'ibm871', 
'ibm880', 
'ibm905', 
'ibm00924', 
'x-cp20936', 
'x-cp20949', 
'cp1025', 
'koi8-u', 
'iso-8859-1', 
'iso-8859-2', 
'iso-8859-3', 
'iso-8859-4', 
'iso-8859-5', 
'iso-8859-6', 
'iso-8859-7', 
'iso-8859-8', 
'iso-8859-9', 
'iso-8859-13', 
'iso-8859-15', 
'x-europa', 
'iso-8859-8-i', 
'iso-2022-jp', 
'csiso2022jp', 
'iso-2022-jp', 
'iso-2022-kr', 
'x-cp50227', 
'euc-jp', 
'euc-cn', 
'euc-kr', 
'hz-gb-2312', 
'gb18030', 
'x-iscii-de', 
'x-iscii-be', 
'x-iscii-ta', 
'x-iscii-te', 
'x-iscii-as', 
'x-iscii-or', 
'x-iscii-ka', 
'x-iscii-ma', 
'x-iscii-gu', 
'x-iscii-pa', 
'utf-7', 
'utf-32', 
'utf-32be' 
); 
public function __construct($url) 
{ 
$this->url=$url; 
} 
//打开网站 
private function open($url) 
{ 
if($this->request!==null) 
{ 
if($this->request->status==200) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 
else 
{ 
$this->request=new Snoopy(); 
$this->request->fetch($url); 
if($this->request->status==200) 
{ 
$this->request->results=strtolower($this->request->results); 
$charset=$this->getCharset(); 
if($charset!="utf-8") 
{ 
if($charset=="windows-1252") 
{ 
$this->request->results=$this->uni_decode($this->request->results); 
} 
else 
{ 
$this->request->results=mb_convert_encoding($this->request->results,"UTF-8",$charset); 
} 
} 
return true; 
} 
else 
{ 
return false; 
} 
} 
} 
//获取网站title,keywords,description 
public function getWebinfo() 
{ 
$info=array( 
'title'=>'', 
'keywords'=>'', 
'desc'=>'', 
'ip'=>'' 
); 
if(!$this->open($this->url)){return $info;exit;} 
// print_r($this->request->results);exit; 
preg_match('/<title>([^>]*)<\/title>/si', $this->request->results, $titlematch ); 
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', $this->request->results, $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', $this->request->results, $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, $limiti=count($names); $i < $limiti; $i++) 
{ 
$metaTags[$names[$i]] = array ( 
'html' => htmlentities($originals[$i]), 
'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",$this->request->results,$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; 
} 
} 
} 
?>
PHP 相关文章推荐
PHP数据缓存技术
Feb 14 PHP
php格式化工具Beautify PHP小小BUG
Apr 24 PHP
PHP中调用ASP.NET的WebService的代码
Apr 22 PHP
探讨:php中在foreach中使用foreach ($arr as &amp;$value) 这种类型的解释
Jun 24 PHP
解析PHP中的内存管理,PHP动态分配和释放内存
Jun 28 PHP
PHP利用str_replace防注入的方法
Nov 10 PHP
php实现转换html格式为文本格式的方法
May 16 PHP
详解Yii实现分页的两种方法
Jan 14 PHP
php出租房数据管理及搜索页面
May 23 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
Jun 27 PHP
PHP实现动态创建XML文档的方法
Mar 30 PHP
PHP连接MySQL数据库并以json格式输出
May 21 PHP
php中经典方法实现判断多维数组是否为空
Oct 23 #PHP
PHP禁止页面缓存的代码
Oct 23 #PHP
Pain 全世界最小最简单的PHP模板引擎 (普通版)
Oct 23 #PHP
供参考的 php 学习提高路线分享
Oct 23 #PHP
PHP中的strtr函数使用介绍(str_replace)
Oct 20 #PHP
PHP中读写文件实现代码
Oct 20 #PHP
Array of country list in PHP with Zend Framework
Oct 17 #PHP
You might like
php 读取shell管道传输过来的内容
2010/03/01 PHP
php使用fopen创建utf8编码文件的方法
2014/10/31 PHP
php支付宝手机网页支付类实例
2015/03/04 PHP
浅谈PHP拦截器之__set()与__get()的理解与使用方法
2016/10/18 PHP
php删除二维数组中的重复值方法
2018/03/12 PHP
PHP hebrev()函数用法讲解
2019/02/21 PHP
NiftyCube——轻松实现圆角边框
2007/02/20 Javascript
jQuery 中关于CSS操作部分使用说明
2007/06/10 Javascript
chrome下jq width()方法取值为0的解决方法
2014/05/26 Javascript
JS判断日期格式是否合法的简单实例
2016/07/11 Javascript
微信小程序模板和模块化用法实例分析
2017/11/28 Javascript
一篇文章介绍redux、react-redux、redux-saga总结
2019/05/23 Javascript
微信小程序实现订单倒计时
2020/11/01 Javascript
Javascript Dom元素获取和添加详解
2019/09/24 Javascript
Vue打包后访问静态资源路径问题
2019/11/08 Javascript
用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
2015/11/05 Python
Python 实现删除某路径下文件及文件夹的实例讲解
2018/04/24 Python
python 识别图片中的文字信息方法
2018/05/10 Python
python解决字符串倒序输出的问题
2018/06/25 Python
使用Python OpenCV为CNN增加图像样本的实现
2019/06/10 Python
Django实现从数据库中获取到的数据转换为dict
2020/03/27 Python
Python 实现自动完成A4标签排版打印功能
2020/04/09 Python
Pytorch数据拼接与拆分操作实现图解
2020/04/30 Python
Python tkinter制作单机五子棋游戏
2020/09/14 Python
Python根据字典的值查询出对应的键的方法
2020/09/30 Python
澳大利亚在线高跟鞋商店:Shoe Me
2019/11/19 全球购物
启动一个线程是用run()还是start()
2016/12/25 面试题
在职人员函授期间自我评价分享
2013/11/08 职场文书
高中生学习生活的自我评价
2013/11/27 职场文书
高一物理教学反思
2014/01/24 职场文书
仓库主管岗位职责
2014/03/02 职场文书
环保建议书300字
2014/05/14 职场文书
电子信息专业应届生自荐信
2014/06/04 职场文书
刘胡兰观后感
2015/06/16 职场文书
您对思维方式了解多少?
2019/12/09 职场文书
oracle索引总结
2021/09/25 Oracle